Results 1 to 12 of 12
  1. #1
    Shien is offline Member
    Join Date
    Dec 2011
    Posts
    16
    Rep Power
    0

    Default 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. #2
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default Re: for loops problem

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

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,608
    Rep Power
    23

    Default 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. #4
    Shien is offline Member
    Join Date
    Dec 2011
    Posts
    16
    Rep Power
    0

    Default 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. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,608
    Rep Power
    23

    Default 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. #6
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default 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. #7
    Shien is offline Member
    Join Date
    Dec 2011
    Posts
    16
    Rep Power
    0

    Default 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. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: for loops problem

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

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,608
    Rep Power
    23

    Default 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 10:23 PM.

  10. #10
    Shien is offline Member
    Join Date
    Dec 2011
    Posts
    16
    Rep Power
    0

    Default 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. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,608
    Rep Power
    23

    Default 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. #12
    Shien is offline Member
    Join Date
    Dec 2011
    Posts
    16
    Rep Power
    0

Similar Threads

  1. I have a problem with my for loops
    By tangel in forum New To Java
    Replies: 0
    Last Post: 10-13-2011, 05:49 AM
  2. Replies: 20
    Last Post: 03-22-2011, 04:02 AM
  3. Help with Loops
    By Spyderpig in forum New To Java
    Replies: 10
    Last Post: 02-17-2011, 08:10 AM
  4. Loops and display problem
    By lk1001 in forum New To Java
    Replies: 6
    Last Post: 02-26-2010, 04:26 PM
  5. Problem with ordering for loops
    By ScaryJello in forum New To Java
    Replies: 3
    Last Post: 03-31-2009, 08:20 PM

Posting Permissions

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