Array Problems
Hi all
This is probably a simple problem for all of you but i can't figure it out. I am designing a Canon program, where the user inputs some text and the program shows the user what words are in the text and how many they appear. I have to be able to sort the results alphabetically. For this i am going to use bubblesort but that requires you to use arrays. The problem is that i don't know how to get the words entered into the array. Here is the code for the program, thanks for any help.
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import java.io.*;
public class Coursework extends JFrame {
private int rval =0;
private int wordLength[] = new int[27];
private JTextArea inputField;
private JLabel prompt;
private JTextArea display;
private JButton goButton;
private JButton openButton;
private JButton saveButton;
private JButton clearButton;
private JButton wordSearchButton;
private JButton wordLengthSearchButton;
private JButton top20Button;
private JButton alphabeticalSortButton;
private JButton profileButton;
private String word;
private String input;
private Object targetKey;
private Object currentKey;
private File file;
private Enumeration keys;
private int numberOfWords = 0 ;
private static long numCopies = 0;
private int Size = numberOfWords;
private String theWords[] = new String[Size];
private int theFreq[] = new int [Size];
private Hashtable table;
private int hardNumber = 0 ;
private int i = 0;
private String output = " ";
public Coursework()
{
super( "Word Type Count" );
inputField = new JTextArea("Input Text Here", 8, 30 );
inputField.setLineWrap(true);
inputField.setWrapStyleWord(true);
table = new Hashtable();
goButton = new JButton( "Run Application" );
goButton.addActionListener(
new ActionListener() { // anonymous inner class
public void actionPerformed( ActionEvent event )
{
createTable();
display.setText( createOutput() );
}
} // end anonymous inner class
); // end call to addActionListener
clearButton = new JButton("Clear");
clearButton.addActionListener(
new ActionListener() { // anonymous inner class
public void actionPerformed( ActionEvent event )
{
table.clear( ) ;
for ( int loop = 0; loop <= 26; loop++) {
wordLength[ loop ] = 0 ;
}
createOutput() ;
display.setText( "" ) ;
inputField.setText( "" ) ;
i = 0 ;
JOptionPane.showMessageDialog( null,
"Cleared Canon ?",
"clear canon?",
JOptionPane.PLAIN_MESSAGE ) ;
}
} // end anonymous inner class
); // end call to addActionListener
openButton = new JButton("Open File");
openButton.addActionListener(
new ActionListener() { // anonymous inner class
public void actionPerformed( ActionEvent event )
{
openFile();
}
} // end anonymous inner class
); // end call to addActionListener
saveButton = new JButton("Save File");
saveButton.addActionListener(
new ActionListener() { // anonymous inner class
public void actionPerformed( ActionEvent event )
{
saveFile(file);
}
} // end anonymous inner class
); // end call to addActionListener
wordSearchButton = new JButton("Word Search");
wordSearchButton.addActionListener(
new ActionListener() { // anonymous inner class
public void actionPerformed( ActionEvent event )
{
int i = 0;
String output = "";
createTable();
String targetKey = JOptionPane.showInputDialog (
"Enter Word to search for");
if ( table.containsKey( targetKey )){
i++;
output+= "No of Words: " + table.get( targetKey ) + "\n";
if ( targetKey.length() > 8 )
{ output += "This is a Hard Word" + "\n";}
else {
output+= "This is not a Hard Word" + "\n";}
JOptionPane.showMessageDialog(null, output);
}
else{
JOptionPane.showMessageDialog(null, "Not Found");
}//end else
}
} // end anonymous inner class
); // end call to addActionListener
wordLengthSearchButton = new JButton("Search By Length");
wordLengthSearchButton.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event )
{
String output = "";
createTable();
Enumeration keys = table.keys();
String length = JOptionPane.showInputDialog(" Enter length of Word");
int wordLengthToFind = Integer.parseInt(length);
while ( keys.hasMoreElements() ) {
String currentKey = (String) keys.nextElement();
if (wordLengthToFind currentKey.length()){
output += "The Word: " + currentKey + " " + "Appears in the Canon: " + table.get( currentKey ) + " times\n";
}
}
if(output.equals(""))
JOptionPane.showMessageDialog(null," Word not found ");
else
JOptionPane.showMessageDialog(null,output);
}
}
);// end ActionListener
top20Button = new JButton("Top 20 Words");
top20Button.addActionListener(
new ActionListener() { // anonymous inner class
public void actionPerformed( ActionEvent event )
{
JOptionPane.showMessageDialog(null, "At the present Time This Function Doesn't Work");
}
} // end anonymous inner class
); // end call to addActionListener
alphabeticalSortButton = new JButton("Sort Alphabetically");
alphabeticalSortButton.addActionListener(
new ActionListener() { // anonymous inner class
public void actionPerformed( ActionEvent event )
{
JOptionPane.showMessageDialog(null, "At the present Time This Function Doesn't Work");
}
} // end anonymous inner class
); // end call to addActionListener
profileButton = new JButton("Word Profile");
profileButton.addActionListener(
new ActionListener() { // anonymous inner class
public void actionPerformed( ActionEvent event )
{
JOptionPane.showMessageDialog(null, "At the present Time This Function Doesn't Work");
}
} // end anonymous inner class
); // end call to addActionListener
prompt = new JLabel( "Enter a string:" );
display = new JTextArea( 25, 30 );
display.setEditable( false );
JScrollPane displayScrollPane = new JScrollPane( display );
// add components to GUI
Container container = getContentPane();
container.setLayout( new FlowLayout() );
container.add( prompt );
container.add( inputField );
container.add( goButton );
container.add( clearButton );
container.add( openButton ) ;
container.add( saveButton ) ;
container.add( wordSearchButton );
container.add( top20Button );
container.add( alphabeticalSortButton );
container.add( profileButton );
container.add( wordLengthSearchButton );
container.add( displayScrollPane );
container.add( new JScrollPane (inputField));
setSize( 600, 650 );
setVisible( true );
} // end constructor
// create table from user input
private void createTable() {
table.clear();
for(int len=0;len<27;len++)wordLength[len]=0;
Enumeration keys = table.keys();
hardNumber = 0 ;
String input = inputField.getText();
StringTokenizer words = new StringTokenizer( input, " \n\t\r" );
while ( words.hasMoreTokens() ) {
numberOfWords ++ ;
String word = words.nextToken().toLowerCase(); // get word
// if the table contains the word
if ( table.containsKey( word ) ) {
Integer count = (Integer) table.get( word ); // get value
// and increment it
table.put( word, new Integer( count.intValue() + 1 ) );
if ( word.length() > 8 ) {
hardNumber ++ ;
}
} else // otherwise add the word with a value of 1
table.put( word, new Integer( 1 ) );
if ( word.length() > 8 ) {
hardNumber ++ ;
}
if (word.length() > 25) {
wordLength[26] ++;
}
else {
wordLength[word.length()] ++ ;
}
} // end while
} // end method createTable
// create string containing table values
private String createOutput() {
String output = "Word\tFreq" + "\n" + "\n";
Enumeration keys = table.keys();
// iterate through the keys
while ( keys.hasMoreElements() ) {
Object currentKey = keys.nextElement();
// output the key-value pairs
output += currentKey + "\t" + table.get( currentKey ) + "\n";
}
output += "size: " + table.size() + "\n";
output += "isEmpty: " + table.isEmpty() + "\n";
output += "Hard Word : " + hardNumber + "\n";
output += "Total number of words = " + numberOfWords + "\n";
// word profile
for ( int loop = 1; loop <= 26; loop++ ) {
output += "\n" + loop +": = " + wordLength[ loop ] ;
}
return output;
} // end method createOutput
private void openFile()
{
// display file dialog, so user can choose file to open
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode( JFileChooser.FILES_ONLY );
int result = fileChooser.showOpenDialog( this );
// if user clicked Cancel button on dialog, return
if ( result JFileChooser.CANCEL_OPTION )
return;
File fileName = fileChooser.getSelectedFile(); // get selected file
// display error if invalid
if ( fileName null || fileName.getName().equals( "" ) )
JOptionPane.showMessageDialog( this, "Invalid File Name",
"Invalid File Name", JOptionPane.ERROR_MESSAGE );
else {
// open file
try {
FileReader load = new FileReader( fileName ) ;
BufferedReader reader = new BufferedReader( load );
String output = "" ;
String inputText = reader.readLine() ;
while( ( inputText = reader.readLine() ) != null)
output += inputText + "\n" ;
inputField.setText( output ) ;
}
// process exceptions from opening file
catch ( IOException ioException ) {
JOptionPane.showMessageDialog( this, "Error Opening File",
"Error", JOptionPane.ERROR_MESSAGE );
}
} // end else
} // end method openFile
public void saveFile(File file) {
try {
JFileChooser chooser = new JFileChooser();
int option = chooser.showSaveDialog(this);
if (option JFileChooser.APPROVE_OPTION) {
if (chooser.getSelectedFile() != null) {
FileWriter fo = new
FileWriter(chooser.getSelectedFile().getAbsolutePa th()+".txt");
System.out.println("got fo object <<<<<< ");
fo.write(this.inputField.getText());
System.out.println("writting done >>>>> ");
fo.close();
JOptionPane.showMessageDialog(this,
"You saved " + chooser.getSelectedFile().getName() +
".txt");
} else {
JOptionPane.showMessageDialog(this, "nothing.");
}
} else {
JOptionPane.showMessageDialog(this, "You canceled.");
}
} catch (Exception ex) {
System.out.println("Error " + ex);
}
}
public void bubbleSort( int array2[] )
{
// loop to control number of passes
for ( int pass = 1; pass < array2.length; pass++ ) {
// loop to control number of comparisons
for ( int element = 0;
element < array2.length - 1;
element++ ) {
// compare side-by-side elements and swap them if
// first element is greater than second element
if ( array2[ element ] > array2[ element + 1 ] )
swap( array2, element, element + 1 );
} // end loop to control comparisons
} // end loop to control passes
} // end method bubbleSort
// swap two elements of an array
public void swap( int array3[], int first, int second )
{
int hold; // temporary holding area for swap
hold = array3[ first ];
array3[ first ] = array3[ second ];
array3[ second ] = hold;
}
public static void main( String args[] )
{
Coursework application = new Coursework();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
} // end class WordTypeCount
|