aspose file tools*
The moose likes Java in General and the fly likes How to pass variable values between successive for loops? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to pass variable values between successive for loops?" Watch "How to pass variable values between successive for loops?" New topic
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: 3681
    
  16
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: 11422
    
  16

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:




There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to pass variable values between successive for loops?