This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

BufferedReader Help

 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,

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,
Thanks
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I were doing it from scratch I'd implement a method that scans a word at a time, and calls an implementation of a "per-word" processor that'd be passed in the method call.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49756
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You realise that StringTokenizer is regarded as legacy code and the API advises you against using it in new code? Use a Scanner to read the input stream and try its next() and nextXXX() and hasNextXXX() methods.

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.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scanner would definitely work, and probably be a lot easier than trying ot use a BufferedReader object, but I think we are supposed to use readers. He drew a UML diagram for us before we started the project and there were two Reader classes: WordReader and CharacterReader. CharacterReader was really easy because of the BufferedReader's read() function. WordReader is turning out to be a little more tricky.

I will, however, try what you are suggesting Campbell; thanks for the help.

-Hunter M
 
Campbell Ritchie
Sheriff
Pie
Posts: 49756
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it say on the UML diagram whether those classes use or extend other classes?
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.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They both implement an interface called MachineReader, which has an abstract read() method. Since they were called CharacterReader and WordReader I just assumed we needed to use Reader objects.


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.

-Hunter M
 
Campbell Ritchie
Sheriff
Pie
Posts: 49756
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the return type of that read() method in the interface?
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the reader interface and the two reader classes I have so far:

Interface:


CharacterReader:



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:


-Hunter
 
Campbell Ritchie
Sheriff
Pie
Posts: 49756
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try a separate method with a while loop, and see how it works.
campbell@queeg:~/java$ java ScannerDemo
Campbell is brilliant.
Campbell
is
brilliant. [***]
Finished.
campbell@queeg:~/java$
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic