• Post Reply Bookmark Topic Watch Topic
  • New Topic

While loop and user input  RSS feed

 
Elias Gomersen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone. This is really a beginner question. If mod sees it and decide it's too basic and to "nooby". Please remove this thread.
Anyway, Ive been learning doing while loops and user input using scanner, but combing them is something I cannot seem to solve. (If you can refer to me a video/website that deals with the same type of solution, i would be really happy!!)

Here is what needs to be done. read a integer from the keyboard over and over until is read 0< number (using WHILE loop)

Here is what ive done:



Thanks !
 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll want to replace your a-- with edd.nextInt(). Every time through the loop you'll want to get another int.

Also, variable names should begin with a lower case character, so it should be edd and not Edd.
 
Elias Gomersen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:You'll want to replace your a-- with edd.nextInt(). Every time through the loop you'll want to get another int.

Also, variable names should begin with a lower case character, so it should be edd and not Edd.
Alright but if i want to now use a text and number, and I want to print the text much as the number specifies, how do I do that? using While
Thanks for your help! Hope you dont mind these noob questions.
 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mixing input as a number and as text potentially exposes you to one of the Scanner's gotchas.
This will not give you what you expect because nextInt() leaves a new-line character in the input buffer and nextLine() will see that and return an empty string. After nextInt() you have to flush the new-line by calling nextLine().
 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Elias Gomersen wrote:Here is what needs to be done. read a integer from the keyboard over and over until is read 0< number (using WHILE loop)

On second reading I find this a poorly phrased requirement that is subject to misinterpretation. "0< number", what number? I assume that the keyboard will be read over and over because the read will be inside the body of the loop.

This needs some clarification.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!