The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Java Math.random() and loop

Could anyone tell me why this loop of mine only run once? So this would run if the user select option "2", the passLength is the length the user input in. I choose 3 as an input, won't let mean my while loop will be running 3 time? As you can see, I put a println there so it print everytime it run, but I only get 1 output.

Re: Java Math.random() and loop

Could anyone tell me why this loop of mine only run once?

Add some println statements inside and at the bottom of the loop that prints out the values of the variable(s) that control the looping in the while statement. Seeing the values of the variables will help you understand what the code is doing.

What are all the "magic" numbers for? 90, 97, 57 and 65
If they are the values of chars then code them as chars: 'A' etc

Re: Java Math.random() and loop

It is doing exactly what it suppose to except the loop stop after 1. I can't seem to figure out why it would just stop after loop one. Then I put a println to print the "LowerLAmount" to see how many time the for loop is runing but it stop after 1.

Re: Java Math.random() and loop

The while loop would print out a value from 65-122. If it encounter a value of 90 to 97, it would keep looping. Ahh.... I know where this is going. I guess because I put since the LowerLAmount can be less than or equal to passLength so when the while loop stop, the for loop also stop?

Re: Java Math.random() and loop

Did you add the println statement i suggested?
What is the value of the while loop's controlling variable that is printed out?
What values in that variable will stop the looping? Was the printed value going to allow the loop to continue?

Re: Java Math.random() and loop

I'm kind of confused by the question, but the while loop will loop to try and print a number from 65-122. The loop will keep looping if it encounter a number from 90-97 so it the value in the variable that will stop the looping is any value from 65-90 and 97-122

Re: Java Math.random() and loop

I'm kind of confused by the question

Can you explain what the confusion is? I suggested you add a println statement inside and at the end of the while statement that prints out the value of the variable the controls the while statements looping.
for example:

Add the println, execute the code and copy the program's output and paste it here.
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Re: Java Math.random() and loop

Please don't use "magic" numbers like 90 and 97. I do not know what char they are supposed to be.
Use a char value like 'A' or '9'.
You are trying to get values from 'A' to 'z' skipping over the values between 'Z' and 'a'.

Re: Java Math.random() and loop

I'll keep in mind about "magic" numbers for the next program , thank you.Yes, as you say, I'm trying to get values from 'A' to 'z' skipping over the values between 'Z' and 'a'. Now that you help me understand why it stop running, I realize my for loop stop because the while loop terminate. Is there anything I could do to make the for loop continue for whatever the value the user might input for "passLength"? Thanks

Re: Java Math.random() and loop

You need to continue debugging your code to see why it is doing what it does.
Add a println statement just before the while statement that prints out the value of the while statement's loop control variable. The same one you are already printing at the end of the while loop.