Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A looping algorithm question?

 
Andrew Parker
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have the following code:

I instantiated an obj and passed 6 parameters, they were written to a text file with PrintOut method in Write class. Since A to F are private variables, I use get methods to return their values.
So, there is a record of 6 data written to a text file.
However, after saving this record, I want the console to ask whether the user wants to input additional record? e.g. System.out.println("Do you want to add more record (Y/N)?"); and it got the "y/n" reply from user. I know how to use BufferedReader console = new BufferedReader(new InputStreamReader(System.in)); to get user input.

However, I do not know where should I put the while/do...while loop as the logic is:
Ask the user to input the first record;
then
Ask the user whether he wants to add more record for continuing input;
A while loop seems not feasible.
Anyone could propose the algorithm?
Thanks a lot
Andrew
 
Vin Kris
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do {} while()
 
Norm Miller
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On the other hand,
Van Rensaleer says that is discouraged.
See 5.1.1 of the Coding standard.
 
James Chegwidden
Author
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can write any loop using a while loop. for loops can be written as while or do/while. Also, do/whiles can be written as for or while as well.
Just watch your logic
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic