# Thread: for loops problem

1. Member
Join Date
Dec 2011
Posts
16
Rep Power
0

## for loops problem

I am making program that would read string lines, separate them and after that check them and write to texAreas.

Java Code:
```BufferedReader br = new BufferedReader(new FileReader(filename));
while((line = br.readLine()) != null)
{
sepArray = line.split(";");
for(int j = 0; j < sepArray.length; j++)
{
//System.out.println(sepArray[j]);
for(int i = 0; i < sepArray.length; i++)
{
if(i%2 == 0)
{
fta = sepArray[i];
sta = sepArray[i+1];
if(qline.equals(fta))
{
if((fta.equals(oldfta)) && (sta.equals(usta)))
{
break;
}
textArea1.append(fta+"\n");
textArea2.append(sta+"\n");
sta = sepArray[i+1];
oldsta = sta;
oldfta = fta;
usta = sta;
}
else if(fta.equals(oldsta))
{
textArea1.append(fta+"\n");
textArea2.append(sta+"\n");
oldsta = sta;
}
}

}
}
}```
The text file has this data:
John is sad;John cries
John's mom is unhappy; She doen't make dinner
John cries;John's mom is unhappy

Now for loop goes only once, but it should go few times to show all data.

I hope you understood what i met

2. Member
Join Date
Oct 2011
Posts
90
Rep Power
0

## Re: for loops problem

Put a System.out.println("something") inside each loop/condition and find out where it is stopping.

3. ## Re: for loops problem

I see two for loops, which one is the problem. How do you know execution enters the while loop?
What is the length of the array that is created?
For debugging the code, Print out the array's contents: Arrays.toString(<THEARRAY>) returns a String you can print.

4. Member
Join Date
Dec 2011
Posts
16
Rep Power
0

## Re: for loops problem

Java Code:
```while((line = br.readLine()) != null)
{
System.out.println("OK");
sepArray = line.split(";");
for(int j = 0; j < sepArray.length; j++)
{
System.out.println(sepArray[j]);
for(int i = 0; i < sepArray.length; i++)
{
System.out.println(sepArray[i]);```
While and first for loop gives this output:
OK
John is sad
John cries
OK
John's mom is unhappy
She doen't make dinner
OK
John cries
John's mom is unhappy

And the output is :
OK
John is sad
John is sad
John cries
John cries
John is sad
John cries
OK
John's mom is unhappy
John's mom is unhappy
She doen't make dinner
She doen't make dinner
John's mom is unhappy
She doen't make dinner
OK
John cries
John cries
John's mom is unhappy
John's mom is unhappy
John cries
John's mom is unhappy

5. ## Re: for loops problem

Is the printed output correct? If there is a problem with it , please add some comments showing what is wrong with it and show what it should be.

What is the length of the array that is created?
Also print out the value of line after it is read:
println("line=" + line + "<");

6. Member
Join Date
Oct 2011
Posts
90
Rep Power
0

## Re: for loops problem

While and first for loop gives this output:
OK
John is sad
John cries
OK
John's mom is unhappy
She doen't make dinner
OK
John cries
John's mom is unhappy
You wanted it to show all the data. It appears that the while and first for-loop do just that. Am I missing something?

7. Member
Join Date
Dec 2011
Posts
16
Rep Power
0

## Re: for loops problem

the main idea of the program is to choose array[i] value (if it's i%2 ==0) and put it to one textarea and then put array[i+1] to another. After that search item which (i%2==0) and is the same as array[i+1] was. And this goes all over while there is connection between string values. And I can't to make array after going one time to the end go again. Can you tell me how to do this? :)

8. ## Re: for loops problem

That much nesting of loops and if statements makes kittens cry. Time to refactor.

9. ## Re: for loops problem

I can't to make array after going one time to the end go again
Your print out shows you read three lines and went into the loops and printed at line 7 and at line 10.

Can you explain what did not happen that should have happened? The printed output shows the code executed inside of both loops.

If you can not understand what your code is doing, you must debug it line by line. If you do not have an interactive debugger, then you must add lots of printlns that show the values of ALL the variables you use and that shows when you enter if statements and for loops.
Make sure that all the printlns have a unique id String so you know which println printed what line on the print out.
Last edited by Norm; 12-06-2011 at 11:23 PM.

10. Member
Join Date
Dec 2011
Posts
16
Rep Power
0

## Re: for loops problem

I am using textfield to search for the string Lets say I input john is sad. when it is found it goes to first texarea and then John cries goes to second. Later john cries goes to first and mom... goes to second. And then search stops because it reached ending. To work correctly loop should start again and append one more value to each area.

11. ## Re: for loops problem

Did you add the printlns to show what your code is doing when it executes? When you do that you should see where the mistake is.
If you can not understand what your code is doing, you must debug it line by line. If you do not have an interactive debugger, then you must add lots of printlns that show the values of ALL the variables you use and that shows when you enter if statements and for loops.
Make sure that all the printlns have a unique id String so you know which println printed what line on the print out.

12. Member
Join Date
Dec 2011
Posts
16
Rep Power
0

## Re: for loops problem

I will look, thanks

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•