| Author |
How to pass variable values between successive for loops?
|
Ziya Bakan
Greenhorn
Joined: Feb 07, 2013
Posts: 13
|
|
I have two successive for loops and i need to pass one of the variables' value to an instance inside the other for loop.
for(int x=0; x< sentence.length(); x++) {
int i;
if (!Character.isWhitespace(sentence.charAt(x)))
i = x ;
break;
}
for (int i ; i < sentence.length(); i++) {
if (Character.isWhitespace(sentence.charAt(i)))
if (!Character.isWhitespace(sentence.charAt(i + 1)))
}
This is just a part of my program and my purpose is assigning the value of x (from the fĂrst for loop) to i variable(from the second for loop) so that i wont start from 0 but from the value of x(before breaking the first for loop)...Looking forward to your replies...Thank you so much in advance!!!
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
|
You could either declare your i variable outside of your first for loop or you could put your second for loop inside the if statement in your first for loop
|
Joanne
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9942
|
|
You may want to search around on the term 'variable scope'. Basically, method variables can (and do) go 'out of scope' when you hit the closing brace of the block where they are defined. So:
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
 |
|
|
subject: How to pass variable values between successive for loops?
|
|
|