aspose file tools*
The moose likes Beginning Java and the fly likes BufferedReader Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "BufferedReader Help" Watch "BufferedReader Help" New topic
Author

BufferedReader Help

Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

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


"If the facts don't fit the theory, get new facts" --Albert Einstein
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Oct 13, 2005
Posts: 38788
    
  23
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

Joined: Mar 13, 2009
Posts: 492

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

Joined: Oct 13, 2005
Posts: 38788
    
  23
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

Joined: Mar 13, 2009
Posts: 492

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

Joined: Oct 13, 2005
Posts: 38788
    
  23
What is the return type of that read() method in the interface?
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

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

Joined: Oct 13, 2005
Posts: 38788
    
  23
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: BufferedReader Help