• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about BufferedReader

 
Shen Lin
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The task is typing in some people's names and the output should be random pairs of these people. e.g.
[awana]abc: java Pairs
james
alex
jane
james and jane
alex
My program is shown below:
When running this program, the method randomPairs(nameSet) cannot be reached since the program cannot break out from the while-loop. I wonder how the method randomPairs(nameSet) can be executed after the while-loop.
Thanks,
Shen
 
Richard Jensen
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shen Lin:
When running this program, the method randomPairs(nameSet) cannot be reached since the program cannot break out from the while-loop. I wonder how the method randomPairs(nameSet) can be executed after the while-loop.
Thanks,
Shen

When the input stream closes then the buffered reader will return null for the readLine.
So, if you run from the command line you need to enter your names (one per line) and then enter the appropriate end of stream character for your OS (on Windows it is Ctrl-Z).
It would also work by redirecting the contents of a file containing one name per line.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic