• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

for loop keep stoping

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For my code, my for loop keep stoping after 1 loop because my while loop keep terminating after it satisfy the condition, how can I make the for loop run more than one? Thanks

 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jay Mize wrote:how can I make the for loop run more than one?


*might be* changing 1 to 0 in your first part of for loop (int LowerLAmount = 1)

to get clear answer please do through this
 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok sorry . So basically I'm trying to generate random uppercase and lowercase letter. "passLength" is a number the user input in. So let say the user put in "3". The program should run the while loop 3 time and generate 3 random uppercase or lowercase letter. The ASCII for A to Z is 65-90 while the ASCII for a-z is 97-122. Hence I have the while loop keep running until it get a value that is not from 90 to 97. The problem is that my for loop stop runing after only looping 1 time. I'm guessing this is due to the "LowerLamount <= passLength" so the LowerLAmount can be 1 and still satisfy the conditions. How can I make it so that the for loop run more than 1 time? Hope this is clearer.

 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suggest you add a print statement in your loop to print out the values of LowerAmount and passLength and a second statement to print out the value of LowerUp so you can see exactly what is happening.

BTW in Java the convention is to start variable names with lower case letters.
 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, I do have println statement but I just wanted to clear them up so it be more clean for you to see. Thanks for the tips of lowercase variable . That the result I get on the bottom, the for loop is only running one for some reason.



LowerUp:V
randomLowUp:86
LowerLAmount:1
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are not printing out passLength.

If you have a problem with a for-loop, print out all the variables related to it. I would print LowerLAmount and passLength at the start and end of each iteration of the loop.
 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow... sorry, it didn't copy the whole thing for some reason. Here goes
LowerUp:s
randomLowUp:115
LowerLAmount:1
passLength:3


 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are printing these values in the inner loop when you should be printing the for loop variables in the outer loop.

BTW The problem is down to the value of randomLowUp (which, as an aside, should probably be a local variable declared inside the for loop).
 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm starting to get what you mean Thank you. Here my updated code and the print out. I'm still trying to figure out why it keep giving me the same char.

LowerUp:R
randomLowUp:82
LowerLAmount:1
passLength:3
LowerUp:R
randomLowUp:82
LowerLAmount:2
passLength:3
LowerUp:R
randomLowUp:82
LowerLAmount:3
passLength:3



 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Put the print statement for the LowerUp variable inside the inner loop and see what happens
 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seem the while loop is only running once, am I correct? So let say the while loop find "j" the for loop is just going to repeat it 3 time?
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It seem the while loop is only running once, am I correct?


More specifically it is only running for the first iteration of the for loop. The question is why?

Look at the code and the print out results and evaluate the while loop condition in your head for the first and second iteration of the for loop.
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
also...we can't tell, but could you be swallowing an exception somewhere? if you ever do this:


(or whatever the exact syntax is), your code may be throwing an exception, which is then ignored. You should always at least print the stack trace when an exception is caught.

I don't know if this applies since we don't have all your code, but it is worth mentioning.
 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:

It seem the while loop is only running once, am I correct?


More specifically it is only running for the first iteration of the for loop. The question is why?

Look at the code and the print out results and evaluate the while loop condition in your head for the first and second iteration of the for loop.



Could it be stopping because since the LowerLAmount can be less than or equal to passLength so once the while loops condition is satisfy once, the for loop can stop running?
 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Fred there is alot of magic number so I didn't want to confuse the reader. I'll post it if you need. Thank you
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Could it be stopping because since the LowerLAmount can be less than or equal to passLength so once the while loops condition is satisfy once, the for loop can stop running?


No, the print out shows the for loop is running 3 times it's the while loop that is only executing on the first iteration of the for loop.

First time through randomLowUp (which you haven't shown the declaration of so I'm guessing here) is a value between 90 and 97 and so the while loop executes until randomLowUp is no longer between these values. For the second and subsequent iterations of the for loop what is the value of randomLowUp and what will happen when the while loop condition is evaluated.
 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:

Could it be stopping because since the LowerLAmount can be less than or equal to passLength so once the while loops condition is satisfy once, the for loop can stop running?


No, the print out shows the for loop is running 3 times it's the while loop that is only executing on the first iteration of the for loop.

First time through randomLowUp (which you haven't shown the declaration of so I'm guessing here) is a value between 90 and 97 and so the while loop executes until randomLowUp is no longer between these values. For the second and subsequent iterations of the for loop what is the value of randomLowUp and what will happen when the while loop condition is evaluated.



I try to reset the value of randLowUp to 92 so the while can continue, and it seem to work, is this an approach to this? Do you have other idea on how else I can do it?

 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I try to reset the value of randLowUp to 92 so the while can continue, and it seem to work, is this an approach to this?


What do you mean by "it seems to work". Do you not understand why this works?

To answer your question it is one approach to solving the problem. I can't say it's a good one because personally I wouldn't generate the letter like this in the first place. Aside from that, if you are going to set randomLowUp to 92 I suggest you do it at the beginning of the for loop and preferably declare randomLowUp locally within the for loop.

BTW are you sure you want to do:

What letter is choosen if first time through the random number is say 96 and so the while loop runs again and the next random number is say 121.
 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, thanks to you, I understand why it work. I say " It seem to work" because I know it a very bad way to generate random letter. But I can't use array or .charAt, so Math.random() is the only way I have right now. As for your question of +=, here my complete code. ( If you have other idea of how to generate random uppercase and lowercase letter beside using array/.charAt, please give me a hint ). I know this is a bad approach because it still generate other ASCII beside A-Z and a-z, hence "LowerUp:M_FCjpNNEIjpaHUSSIwoHfPPTCf_AEUGYRHKdLrtYpDOqqmOAywx" ( One of the test run I just did)

 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Do you have other idea on how else I can do it?


I would generate a random number between the int values of 'A' and 'Z' (inclusive).
Then I would generate another random number which would be either 0 or 1 and if it is 0 return the 'A' to 'Z' character else add the difference between the int value of 'a' and 'A' and return the 'a' to 'z' value
 
Tony Docherty
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I know this is a bad approach because it still generate other ASCII beside A-Z and a-z


That's because of the +=
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An alternate way of generating the letters is to put all the possible letters into an array and generate random numbers from 0 to the length of the array-1 and use that number to get the letter from the array.
 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:

I know this is a bad approach because it still generate other ASCII beside A-Z and a-z


That's because of the +=



Since LowerUp is a string, won't += just keep adding the "char" to it? Like for the first run (char)(randomLowUp) give me "A", then the second it give me "B", won't the += just add the two together and make AB? ( I'm not strong in my Java basic operator) . I'll try for suggestion of the generating of letter right now thank you. ( Array is what I'm learning next )
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Run code that looks like this:
System.out.println("just before loop, passLength is " + passLength);

When you do this, you can tell exactly how many times your for-loop runs, what the values are at the beginning of each iteration, and the end.

Also, your code is poorly formatted. it is hard to tell by looking at it what code is part of what block. It starts off well, but at line 29 it starts to go to hell...

edit: so...I just ran your code with the above code. It turns out, your for loop IS running more than once. when i input "2" and then "5", the for loop did run five times. However, the output was only one character.

so that implies that the WHILE loop isn't running on successive iterations of the for-loop.
 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fred, can I get some tips on how I can format my code to make it look better? Thanks
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jay Mize wrote:Fred, can I get some tips on how I can format my code to make it look better? Thanks


ALWAYS use curly brackets, even if they aren't strictly required (line 30).

Be consistent with your bracket positions. On lines 5/6, you have the bracket line up with the line above. on lines 30/31, you indent the bracket...and then indent the body again.

Also, through most of the code, you put the brackets on lines by themselves. then, on line 39, you have a bracket and an assignment.

Line 40 is indented one more space than the rest of the block.

The curly brace on line 44 should be indented the same amount as its matching brace on line 39.

things like that. (line numbers refer to your post made at 13:22:22)
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Since LowerUp is a string, won't += just keep adding the "char" to it?


Sorry, didn't realise LowerUp was a String, I assumed it was a char.
Yes it will keep appending the letters to it but this means you also append letters you don't want. If you move the line out of the while loop it will only append letters in the range you require.
 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this how I am suppose to move it? I still get ASCII inside of 90~97.

 
Tony Docherty
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at your code, you haven't moved it you have copied it.
 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help today, Tony and Fred!
 
reply
    Bookmark Topic Watch Topic
  • New Topic