• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trying to use a while loop with input.  RSS feed

 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my code so far. I am trying to get the WHILE LOOP to work so the user inputs a number, the if statement prints the output and then it returns to ask for another number and goes again and again looping :



[HENRY: Added Code Tags]
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David De wrote:


Java requires that local variables be initialized before it can be used. At this point, the nmbr variable has not been initialized (set) yet.

Henry
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought I initialized it earlier

"double nmbr;"

Is that what you mean?
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, gotcha,
I made it
nmbr = 0;

that worked! thank you
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, that just declares the variable, but it does not initialize it to any value yet. You have to assign a value to it, for example:

Note that there is a difference between local variables (inside a method) and member variables (at class level). Local variables must be initialized explicitly, but member variables are automatically initialized with a default value when not initialized explicitly.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
…and what will happen if you enter 5.5?
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for both the quickness or your reply and your thoroughness as well. I did fix it to what you said.
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am creating two input fields with the scanner. The first one works great and the user input the number, the second one works, but it does not place the cursor at the end of the line "Enter second number here" Any reasons?
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please show us the code you have at present.
Are you using nextLine anywhere? Are you calling nextAnythingElse twice?
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I edited your post to remove some unnecessary blank lines and add code tags: awlays use the tags. Doesn't it look better
I didn't however correct the inconsistent indentation; you can get errors difficult to understand if your indentation is inconsistent. Also always use {} after if else for while etc. We have some suggestions about code formatting here. Also have a look at my suggestions about text editors: start here and look at our FAQ. If you set the right options on the right text editor, it will do all the hard work of indentation for you
The name of your class is difficult to read; it should have CapitalLetters in.
Why have you got “naked” break; statements in your loops? If you are going to use break; it should be wrapped in an if. Otherwise your loops will never repeat, and thisis equivalent to
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!