• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

While loop  RSS feed

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, when I run the program below nothing is being printed to the screen, except for "press any key to continue"



Basically I want it to keep looping asking for a number while the number is greater than or equal to 5 and if the number entered is less than 5 the user breaks out of the loop.
Any ideas? I can't see where the problem is and  it driving me mad. thanks.
 
Saloon Keeper
Posts: 4767
52
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 4 you set num to zero.  On line 6 you look to see if num (zero) is greater to or equal to 5. It is not. So the loop is  never entered.
 
Kevin O'Sullivan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Line 4 you set num to zero.  On line 6 you look to see if num (zero) is greater to or equal to 5. It is not. So the loop is  never entered.



Hmm as soon as I changed the value from 0 to 10 it worked how I wanted it too, it wouldn't work though unless I declared num with a value higher than 5,I understand why that is but this doesn't seem right, how can you be truly testing what the user is entering if num already has a value? do you know what I mean?
 
Carey Brown
Saloon Keeper
Posts: 4767
52
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might be better off with a do-while loop which tests the value at the bottom of the loop.
 
Carey Brown
Saloon Keeper
Posts: 4767
52
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:You might be better off with a do-while loop which tests the value at the bottom of the loop.

Note that you won't need a 'break' with this approach, it will just exit the loop. At which point you can say "Bye".
 
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
while.PNG
[Thumbnail for while.PNG]
output(as per your need my friend)
 
Kevin O'Sullivan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks carey
 
Carey Brown
Saloon Keeper
Posts: 4767
52
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kevin O'Sullivan wrote:Hmm as soon as I changed the value from 0 to 10 it worked how I wanted it too, it wouldn't work though unless I declared num with a value higher than 5,I understand why that is but this doesn't seem right, how can you be truly testing what the user is entering if num already has a value? do you know what I mean?


First off, there's nothing wrong with setting it to a value that will result in the while() letting execution enter the loop. This is not uncommon. Unless otherwise instructed, statements are executed in order. So if you get user input and assign it to num, then num will contain that value until such time as it is assigned to again.
 
Kevin O'Sullivan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:

Kevin O'Sullivan wrote:Hmm as soon as I changed the value from 0 to 10 it worked how I wanted it too, it wouldn't work though unless I declared num with a value higher than 5,I understand why that is but this doesn't seem right, how can you be truly testing what the user is entering if num already has a value? do you know what I mean?


First off, there's nothing wrong with setting it to a value that will result in the while() letting execution enter the loop. This is not uncommon. Unless otherwise instructed, statements are executed in order. So if you get user input and assign it to num, then num will contain that value until such time as it is assigned to again.



Cheers makes sense.
 
Marshal
Posts: 60142
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nishant bhangale, welcome to the Ranch

I am afraid what you posted isn't good code. Apart from the fact that you have far too much code in the main() method, the loop index being different from the number entered and the use of multiple ifs makes for code awkward to read. Read the whole thing aloud and you will probably see what I mean. Also, try to use < and > rather than <= and >=; the one‑character operators are easier to read and understand.
 
Saloon Keeper
Posts: 1037
43
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nishant bhangale,

Welcome to CodeRanch again 

Seems you fumbled while putting code tags around your code, since you're new here this time I edited your code. You can go through --> UseCodeTags to know how to put code tags properly around the code. Don't forget to utilize preview button to make sure how It looks prior posting.

Class name always starts with capital letter ... Go through --> Naming Conventions
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kevin O'Sullivan wrote:Hi, when I run the program below nothing is being printed to the screen, except for "press any key to continue"



Basically I want it to keep looping asking for a number while the number is greater than or equal to 5 and if the number entered is less than 5 the user breaks out of the loop.
Any ideas? I can't see where the problem is and  it driving me mad. thanks.



Instead of declaring a number, you can start with having the user enter a number when the program opens, and then going into the while loop.  Like this...




This way, it will ask upon start, and skip the loop if a number entered is less than 5.
 
 
Kenneth Milota
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kevin O'Sullivan wrote:Hi, when I run the program below nothing is being printed to the screen, except for "press any key to continue"



Basically I want it to keep looping asking for a number while the number is greater than or equal to 5 and if the number entered is less than 5 the user breaks out of the loop.
Any ideas? I can't see where the problem is and  it driving me mad. thanks.



Instead of declaring a number, you can start with having the user enter a number when the program opens, and then going into the while loop.  Like this...




This way, it will ask upon start, and skip the loop if a number entered is less than 5.
 
 
Kenneth Milota
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For some reason, they don't allow you to edit or delete posts on this forum, so I re-posted the correct code 2nd.
 
Campbell Ritchie
Marshal
Posts: 60142
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kenneth Milota wrote:. . . Instead of declaring a number,

All variables must be declared before they can be used in Java┬«. I think you want a different word from “declaring”.

you can start with having the user enter a number when the program opens, and then going into the while loop.  Like this...




This way, it will ask upon start, and skip the loop if a number entered is less than 5.
 

Please indent that code correctly. Then you will know under which circumstances it will print, “Bye”. Also, don't use so many empty lines; they don't actually make the code more legible.
 
Campbell Ritchie
Marshal
Posts: 60142
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kenneth Milota wrote:For some reason, they don't allow you to edit or delete posts on this forum . . . .

We used to have no end of trouble with people “correcting” the point they had just asked about, which made the people replying look stupid.Making threads a bit longer was a small price to pay for getting rid of that sort of annoyance.
 
Kenneth Milota
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

  Please indent that code correctly. Then you will know under which circumstances it will print, “Bye”. Also, don't use so many empty lines; they don't actually make the code more legible.

Are you saying that the syntax of the code I put in won't work, or that it's not readable?
 
Carey Brown
Saloon Keeper
Posts: 4767
52
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes and yes. You have typos, missing braces, and poor formatting.
 
Ganesh Patekar
Saloon Keeper
Posts: 1037
43
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
 
Campbell Ritchie
Marshal
Posts: 60142
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kenneth Milota wrote:. . . Are you saying that the syntax of the code I put in won't work, or that it's not readable?

The real problem is, if your code were readable, you would find the logic errors for yourself. There are some syntactic errors, too.
 
Carey Brown
Saloon Keeper
Posts: 4767
52
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kenneth Milota wrote:Are you saying that the syntax of the code I put in won't work?

The ideal thing to do in this forum with small pieces of code is to create a small class with a main() that contains the code, and then post that. It this way anyone trying to help can quickly compile and run it so that responses can be formulated quickly. It has the added benefit that you can compile it as well and catch errors before you post it. Proper formatting is vital to assist in catching errors, for you as well as us.
 
Carey Brown
Saloon Keeper
Posts: 4767
52
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kenneth Milota wrote:Instead of declaring a number, you can start with having the user enter a number when the program opens, and then going into the while loop.  Like this...

I don't like this approach because you have redundant code that you have to maintain in sync. Specifically lines 3&4 along with 6&7. In this case it is trivial, but I've seen people do this with large blocks of redundant code. So, don't get in the habit of doing this.

With this formatting can you spot the error?
 
Greenhorn
Posts: 4
Chrome Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A closing curly brace is missing for the if statement, or the while loop!

Also, do y'all recommend doing a "sc.nextLine()" after scanning in an int? I was taught that this was very important.
 
Campbell Ritchie
Marshal
Posts: 60142
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

William Varner wrote:A closing curly brace is missing for the if statement, or the while loop! . . .

Or maybe, there is a closing brace which has dropped off the end of the code block. I had been imputing such a } somewhere. But wherever that brace is, the loop is written incorrectly and we don't know whether KM knows where the errors are.

I think the question about Scanners is unrelated to this thread and merits a discussion of its own: here it is
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!