• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

BufferedReader Help

 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 75874
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 75874
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 75874
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 75874
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic