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
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: 3169
    
  10
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: 10916
    
  12

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?
 
Similar Threads
why does this code work
String index out of range
Problems with counting short and long words
Help with english to morse code program, I have no idea what I'm doing
Reverse the words in a string