I need to read input from standard input one word at a time. This is kinda what I had in mind, but I'm not sure this will work because I need to return one word at a time to the calling class.
Here's what I have:
And this is where I am calling this method:
Any help would be great,
You cannot return one word at a time from a method, because a method only ever returns one thing (or void). You would have to set up a loop in the calling method and return some signal value, maybe null, to signify the end of input. Rather iffy design, I think. Maybe easier to add the words to a List<String> and return the List, which the calling method can investigate to its heart's content.
I will, however, try what you are suggesting Campbell; thanks for the help.
Beware of the read() methods which appear to read a single character; they don't quite do what you think. They will for example read return and new line.
Yea I figured this part out last night when my loop wasn't stopping, I have the characterReader working fine though, just stopped up on wordReader.
WordReader: (my current attempt is using the Scanner Class)
Note: that the Scanner is working fine with the machine, my loop simply isnt stopping. That's what Im stopped up on.
Heres my loop:
At the point labelled [***] I entered ctrl-D which is the end-of-transmission character. If you are on Windows you will probably need ctrl-Z instead.
campbell@queeg:~/java$ java ScannerDemo
Campbell is brilliant.