This week's giveaway is in the Spring forum.
We're giving away four 3 month subscriptions to Marco Behler Videos and have Marco Behler on-line!
See this thread for details.
Win a 3 month subscription to Marco Behler Videos this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java loop with if else  RSS feed

 
Geralt Rivea
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i need help with a loop for my program.  When thevuser inputs an incorrect answer i basically need the program to state the answer is incorrect and then loop back to the question and continue looping until the correct answer is inputed.  Any help will be appreciated.
Untitled.png
[Thumbnail for Untitled.png]
 
Liutauras Vilda
Marshal
Posts: 4270
256
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Do you know how to write while loop? I think that would be handy here. Construct info can be found in Java Tutorials, here, give a try and report back how it went.
 
Tony Docherty
Saloon Keeper
Posts: 3262
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

When posting code please copy and paste it into your post (remembering to UseCodeTags) rather than attaching an image. It's much easier for us to read the code and comment on it when it is posted inline and hence much more likely you will get an answer.
 
Geralt Rivea
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Welcome to the Ranch

Do you know how to write while loop? I think that would be handy here. Construct info can be found in Java Tutorials, here, give a try and report back how it went.



Thanks for the quick reply, i did try using the while loop but i get an infinite loop is there any reason why this happens? 
 
Liutauras Vilda
Marshal
Posts: 4270
256
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, because loop's condition never evaluates to false, hence doesn't stop.

Would you mind to show us what have you tried, so we could see better?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12515
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a while loop has some kind of boolean condition (i.e. "true" or "false").  As long at the condition evaluates to true, the loop will continue forever.

That means that somewhere in your loop, you have to have something that makes that condition false...So at a VERY high level, it looks somethint like this:

 
Geralt Rivea
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my code the loop does not work, i think i placed it wrong.

 
Liutauras Vilda
Marshal
Posts: 4270
256
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Loop does work, perhaps not in the way you want it to work.

Think for a moment, when you enter else part and start loop, do you ever give user a chance to re-enter password?

By the way, your indentation is off. That needs to be fixed.
 
Norm Radder
Rancher
Posts: 2052
26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also the {}s are missing for the else statement.  You should always use {} with if, else and loops.
 
Liutauras Vilda
Marshal
Posts: 4270
256
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:Also the {}s are missing for the else statement.  You should always use {} with if, else and loops.

@OP
What Norm pointed out, it suggests that you ought to fix indentation, as you may or may not notice missing braces mistake, which can lead to logical errors.
 
Liutauras Vilda
Marshal
Posts: 4270
256
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@OP

Do you really write code indented like that? Or you using Microsoft Word (sorry for swearing) for code writing? Can you add screenshot of your environment, I would want to see it how it looks there.

Indentation, formatting, variables naming are equally important as logical problem's solving. Pay attention to that always.
 
Campbell Ritchie
Marshal
Posts: 54909
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch (again )
The above shows what your code ought to have looked like with corrected indentation. There is no need for so many empty lines; they don't enhance legibility at all.
Apart from the fact that you have not copied the code exactly, you shou‍ld now be able to see the structure of your code. You need the indentation for yourself; as you have seen you get confused if you can't see the structure of your own code.
Print that code on a piece of paper and go through the code with a pencil and see what the flow of execution is, with a correct password and an incorrect one. Or zoom in to the website so it appears larger and follow it with your finger on the screen. You shou‍ld then be able to see what you are doing wrong.
Also write down the values of the fields password and passwordinput. The latter would have been better as passwordInput. See whether they change their values as you run the program. Note that passwordinput, as a local variable, has no value not even null before line 10. It does have a value after the input dialogue call.
 
Geralt Rivea
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:. . . Welcome to the Ranch (again )
The above shows what your code ought to have looked like with corrected indentation. . . .



Thanks again, it seems that when i pasted the code on the site the indentation went of and i didn't realize it.  I still can't seem to find what's wrong. Im new to java and i can't get this loop to work is it possible for you to tell me what I did wrong. Thanks for your help.
 
Campbell Ritchie
Marshal
Posts: 54909
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is usually no need to quote the whole of a previous post; I have shortened it.
You are going to have to go slowly. Go through the code as I suggested with pencil and paper (or finger and screen) and be sure to remember the values of the two variables for each line. If you can't work it out, you will have to write something like:-
line 4: number = 123
line 5: Try to enter loop. Is number < 99? No. Won't enter loop
loop ends at line 21, so next line is 22
line 22: number = 123 Print that number.
Only that is for a different program.

If you are still stuck, post what you wrote and let us see it.
 
Geralt Rivea
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I finally got it working,  i started over and did everything step by step. Thanks to everyone who helped!!
 
Campbell Ritchie
Marshal
Posts: 54909
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done sorting it out Please show us your solution.
 
Geralt Rivea
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Well done sorting it out Please show us your solution.



Thanks here's the solution.

 
Liutauras Vilda
Marshal
Posts: 4270
256
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fix indentation first, so we could look further.

But from short glance, it is wrong.
 
Geralt Rivea
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Fix indentation first, so we could look further.

But from short glance, it is wrong.


The indentation keeps going off when i post it for some reason.
 
Campbell Ritchie
Marshal
Posts: 54909
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The indentation shown on the display is the same as in the code you posted. Line 37 for example has exactly the same indentation as your post. Please explain exactly what happens, so we can tell whether it is a problem with jGrasp or with this website.
Incorrect indentation makes it difficult to read the code.
 
Knute Snortum
Sheriff
Posts: 3842
91
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no reason to surround this line of code with bare braces:

There is another example at lines 21-24.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!