Anyone who can look at my code and help me understand how to count the word frequencies in the sorted array I have created will be admired forever!!! I have coded for hours and hours, but nothing is working---I am absolutely clueless where to go next, and using the Comparator Interface is puzzling me to no end!!!
Thank you in advance for your kind assistance!!!
As posted 5/4/03: Sir/Madame:
I am working on a program that is to take user text, put it in a sorted array, then display words and their frequencies. I am getting stuck on the frequencies-- I assume I need to make an int[], but am confused how to return both the word(String) and frequency(int) to the tester program. I visualize a two dimensional array, but cannot pull it together for coding. I have read the postings for word counts and the recently answered Set question Duane posed, but I am still unsure how my code should look.
public int wordCount() { st = new StringTokenizer(string, " \t\n\r\f.?,;:!"); return st.countTokens(); }
public String listWordsInOrder() { String returnString = ""; st = new StringTokenizer(string, " \t\n\r\f.?,;:!"); String[] sa = new String[wordCount()]; int index = 0;
for(int i = 0; i < sa.length; i++) { returnString = returnString + sa + "\n"; }
return returnString; }
}
CODE FOR TESTER PROGRAM: public class StringInfoTester extends Object {
public static void main(String[] args) { StringInfo si = new StringInfo("Hello, my name is Victor. \n What is your name?"); System.out.println(si.listWords() + "\n"); System.out.println("There are " + si.wordCount() + " words in the above list."); System.out.println(" "); System.out.println("The sorted list is: "); System.out.println(si.listWordsInOrder()); } }
I am also wondering how I might use the Comparator Interface with this program.
import java.util.*;
publicclass StringInfo
{
private StringTokenizer st;
private String string;
public StringInfo(String string)
{
this.string = string;
}
public String listWords()
{
st = new StringTokenizer(string, " \t\n\r\f.?,;:!");
String returnString = "";
while(st.hasMoreTokens())
{
returnString = returnString + st.nextToken() + "\n";
}
return returnString;
}
publicint wordCount()
{
st = new StringTokenizer(string, " \t\n\r\f.?,;:!");
return st.countTokens();
}
public String listWordsInOrder()
{
String returnString = "";
st = new StringTokenizer(string, " \t\n\r\f.?,;:!");
String[] sa = new String[wordCount()];
int index = 0;
while(st.hasMoreTokens())
{
sa[index++] = st.nextToken();
}
Arrays.sort(sa);
for(int i = 0; i < sa.length; i++)
{
returnString = returnString + sa[i] + "\n";
}
return returnString;
}
publicstaticvoid main(String[] args)
{
StringInfo si = new StringInfo("Hello, my name is Victor. \n What is your name?");
System.out.println(si.listWords() + "\n");
System.out.println("There are " + si.wordCount() + " words in the above list.");
System.out.println(" ");
System.out.println("The sorted list is: ");
System.out.println(si.listWordsInOrder());
}
}
What I have posted above is the code which is compiling. And also I did some changes so that you can see the sorted list. The line I changed was
returnString = returnString + sa[i] + "\n";
As you can see it has not got sorted properly since in String comparison "A" is less than "a". This way of comparing leads to something like so basically a word "It" will be less than "at" but it should be other way around. To over come this when you read the string convert all the chars to CAPS and then sort them.