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.
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?
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:
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.
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 should 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 should 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.
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.
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:-
Only that is for a different program.
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.
If you are still stuck, post what you wrote and let us see it.
Incorrect indentation makes it difficult to read the code.