Results 1 to 12 of 12
Thread: for loops problem
- 12-06-2011, 09:36 PM #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.
The text file has this data: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; } } } } }
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
- 12-06-2011, 09:44 PM #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.
- 12-06-2011, 09:46 PM #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.
- 12-06-2011, 09:59 PM #4
Member
- Join Date
- Dec 2011
- Posts
- 16
- Rep Power
- 0
Re: for loops problem
While and first for loop gives this output: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]);
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
- 12-06-2011, 10:05 PM #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 + "<");
- 12-06-2011, 10:10 PM #6
Member
- Join Date
- Oct 2011
- Posts
- 90
- Rep Power
- 0
Re: for loops problem
You wanted it to show all the data. It appears that the while and first for-loop do just that. Am I missing something?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
- 12-06-2011, 10:16 PM #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? :)
- 12-06-2011, 10:19 PM #8
Re: for loops problem
That much nesting of loops and if statements makes kittens cry. Time to refactor.
- 12-06-2011, 10:21 PM #9
Re: for loops problem
Your print out shows you read three lines and went into the loops and printed at line 7 and at line 10.I can't to make array after going one time to the end go again
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 10:23 PM.
- 12-06-2011, 10:27 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.
- 12-06-2011, 10:31 PM #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-06-2011, 10:32 PM #12
Member
- Join Date
- Dec 2011
- Posts
- 16
- Rep Power
- 0
Similar Threads
-
I have a problem with my for loops
By tangel in forum New To JavaReplies: 0Last Post: 10-13-2011, 05:49 AM -
[Semi-Beginner] (nested loops) What's wrong with my code? (nested loops)
By Solarsonic in forum New To JavaReplies: 20Last Post: 03-22-2011, 04:02 AM -
Help with Loops
By Spyderpig in forum New To JavaReplies: 10Last Post: 02-17-2011, 08:10 AM -
Loops and display problem
By lk1001 in forum New To JavaReplies: 6Last Post: 02-26-2010, 04:26 PM -
Problem with ordering for loops
By ScaryJello in forum New To JavaReplies: 3Last Post: 03-31-2009, 08:20 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks