• Post Reply Bookmark Topic Watch Topic
  • New Topic

Yes/no repeat  RSS feed

 
Johnny Graves
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm in dire need of some help. I've been racking my brain the last few hours trying to figure how incorporate a while  yes / no  repeat function at the end of the program .  Not sure sure how to do this, been scouring my book and notes and haven't found any info. Any help is appreciated. I've seen an example in my book, but im having trouble incorporating it to my program.

Something along these lines would favorable.


         System.out.println("Would you try another number?"
     System.out.print("Enter Y for yes or N for no: ");
         input = keyboard.nextLine();  // Read a line.
         repeat = input.charAt(0);     // Get the first char.

      } while (repeat == 'Y' || repeat == 'y');



 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you put that sort of code at the end of a do loop, it shou‍ld work. Try toLowerCase before you get the charAt(0); then you only need to deal with 'y'.
Please tell us what your book says nextLine does; I haven't seen one that gets it right. (And which book it is.)
 
Johnny Graves
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My book says that nextLine method is used to read an entire line of text, and that it usually ends with the enter key pressed.

 
Johnny Graves
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What gripes me is that I have been looking at my book and there aren't many examples trying to use this method.
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell and I have different thoughts on this subject (I believe), but I'll give you mine.

Scanner#nextLine() reads to the newline character (OS specific) and returns everything it read up to the newline.  Then is consumes the newline.  That sentence is important.  Scanner#nextInt() and all the other nextWhatever() methods do not.  If you have two nextInt() methods after each other you're okay:

However, if you try to get a string after getting an int, this happens:

So my solution is this:
 
Johnny Graves
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So would it be something like this...

 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Campbell and I have different thoughts on this subject (I believe) . . .
It looks as if Campbell and you have exactly the same thoughts about nextLine, but express them slightly differently

Please tell us which book it is. Even people like Cay Horstmann get confused about nextLine. It does not look for the next line at all, but it does something which you will find about in two locations. One is here, and the other is the API documentation, which tells you quite clearly
This method returns the rest of the current line, excluding any line separator at the end.
It also says it positions itself at the beginning of the next line, which Knute expressed as consuming the line end characters.

Well, maybe there are books and other fora which tell you what nextLine does, but I am going to blow my own trumpet and tell you what a good website you have landed on
There are two good solutions to the nextLine problem. The simpler is what Knute has shown you. The more complicated one is to write yourself a utility class for inputs, with a method like that shown in this thread.
I made a serious mistake in that thread. You shou‍ld therefore read the whole thread to find the solution.
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnny Graves wrote:So would it be something like this...

That's what I would do, yes.
 
Johnny Graves
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:
Johnny Graves wrote:So would it be something like this...

That's what I would do, yes.


In order to use this code, would i have to use it in a do while statement. Cause trying to use it alone cause several errors.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just saying "trying to use it alone cause several errors" is not helping us to help you. Please post the errors messages so we can see what is going wrong.

To answer your question yes you have to put the code in a loop of some form or it won't repeat but putting it in a loop won't necessarily solve the errors you are seeing.

BTW the question "Would you like to average another set of test scores? " suggests a yes/no answer so why are you expecting an integer?
 
Johnny Graves
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The last part of code is from another program from my book. I tried explaining that on another forum, but they couldnt understand what i was talking about. So to cut out the misinformation i just typed out that part of the code.

Long story short, I need the program start again if the user chooses to.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok in that case you need to put all the code that needs to repeat inside a loop and at the end of the code (but still inside the loop) you need to ask the user if they want to continue and then get their answer. The loop condition then tests whether or not they answered in the affirmative.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!