Reading characters from a simple output to list words.
Hello there,
It's been a little while since I used Java and I'm trying to brush up on my skills, I have the following problem.
I have a string in the SimpleCharacterReader class which is like the following:
Code :
"Word word, wOrd word word worD\n" +
"a few more words, wordS, yes more words\n" +
SimpleCharacterReader has a method called getNextChar() which does what you'd expect, returns the next character in the sequence. What I am trying to do is create a list of word frequency so for the above
word - 6
words - 3
etc etc
Now currently I have the string being read as follows:
Code :
CharacterReader characterReader = new SimpleCharacterReader();
StringBuilder string = new StringBuilder();
try{
while(true){
string.append(characterReader.getNextChar());
}
} catch(EOFException e) {
try{characterReader.close();} catch(IOException ignore){}
}
Scanner sc = new Scanner(string.toString());
The issue I have with this, is while it works for my little example, I want it to be able to count the words as it reads them, rather than creating a full string then counting. Are there any clean ways of doing this or do I have to just brute force it, add the characters until a delimiter is detected then calling that a word and putting it in the table?
Sorry if that sounds a bit silly.
Re: Reading characters from a simple output to list words.
I would suggest the iterator class, using the hasNext and next methods of the class.
Re: Reading characters from a simple output to list words.
....and if you need more assistance than that, provide more details on your project.
The origin of the string (or data) you plan to work on. Perhaps one of the collections would be a better way to work on your data than a string, perhaps not.
Do you just need to count the number of times each word appears?
Your example seems to show a non-case sensetive setup, remember java is case sensetive.
Do you just need to output the results as in on screen, or save them for later use?
How many times will you have to work on the same data?
Any other factor(s) which may change a best approach to this project...
Re: Reading characters from a simple output to list words.
...oh one last thing I would like to mention, one of the java collections, a map type, has the natural ability to save each key once and only once. With your data in such a map, you could ask if the key (each word in your project) already exists, and if so, ++ the value stored next to the key. On the other hand, if not, add it, and start the value behind the key (in your project, the number of times the word appears) to 1;