wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes for loop keep stoping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "for loop keep stoping" Watch "for loop keep stoping" New topic
Author

for loop keep stoping

Jay Mize
Ranch Hand

Joined: Dec 23, 2012
Posts: 47
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

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Dec 23, 2012
Posts: 47
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.

Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2158
    
  47
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

Joined: Dec 23, 2012
Posts: 47
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
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11150
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jay Mize
Ranch Hand

Joined: Dec 23, 2012
Posts: 47
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

Joined: Aug 07, 2007
Posts: 2158
    
  47
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

Joined: Dec 23, 2012
Posts: 47
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

Joined: Aug 07, 2007
Posts: 2158
    
  47
Put the print statement for the LowerUp variable inside the inner loop and see what happens
Jay Mize
Ranch Hand

Joined: Dec 23, 2012
Posts: 47
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

Joined: Aug 07, 2007
Posts: 2158
    
  47
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
Bartender

Joined: Oct 02, 2003
Posts: 11150
    
  16

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

Joined: Dec 23, 2012
Posts: 47
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

Joined: Dec 23, 2012
Posts: 47
@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

Joined: Aug 07, 2007
Posts: 2158
    
  47
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

Joined: Dec 23, 2012
Posts: 47
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

Joined: Aug 07, 2007
Posts: 2158
    
  47
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

Joined: Dec 23, 2012
Posts: 47
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

Joined: Aug 07, 2007
Posts: 2158
    
  47
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

Joined: Aug 07, 2007
Posts: 2158
    
  47
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

Joined: Aug 07, 2007
Posts: 2158
    
  47
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

Joined: Dec 23, 2012
Posts: 47
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
Bartender

Joined: Oct 02, 2003
Posts: 11150
    
  16

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

Joined: Dec 23, 2012
Posts: 47
Fred, can I get some tips on how I can format my code to make it look better? Thanks
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11150
    
  16

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

Joined: Aug 07, 2007
Posts: 2158
    
  47
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

Joined: Dec 23, 2012
Posts: 47
Is this how I am suppose to move it? I still get ASCII inside of 90~97.

Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2158
    
  47
Look at your code, you haven't moved it you have copied it.
Jay Mize
Ranch Hand

Joined: Dec 23, 2012
Posts: 47
Thanks for your help today, Tony and Fred!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: for loop keep stoping
 
Similar Threads
Stoping all threads in a ThreadPoolExecutor
Stoping a Thread when is not needed anymore
Exceptions stoping my program.
Quartz job stoping after some time
Stoping multiple threads