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

    Default show elements in correct order

    So I have two arrays with values iArray {A,C,E,B,H,C,B,F,D} and tArray{M,D,F,E,M,H,C,A,K}
    My code shows results like this (if I choose letter B):
    E,F
    C,D
    H,M

    But it shoud be like this:

    E,F,A,M
    C,D,K
    H,M

    Can you tell me how to fix it?

    Java Code:
    for(int z = 0; z < counter; z++)
                        {
                            c:
                            for(int i = 0; i < counter; i++)
                            {
                                fta = iArray[i];
                                sta = tArray[i];
                                if(fta.equals(oldsta))
                                {
                                    textArea2.append(sta+" ");
                                    oldsta = sta;
                                    oldfta = fta;
                                    kk++;
                                    finalArray[i] = tArray[i];
                                    index = i;
                                    iArray[i] = "0";
                                    tArray[i] = "0";
                                    break c;
                                }
                                else if(sequence.equals(fta))
                                {
                                    if(index != 0)
                                    {
                                        textArea1.append(finalArray[index]+"\n");
                                        textArea2.append("\n"+sta+" ");
                                        oldsta = sta;
                                        oldfta = fta;
                                        kk++;
                                        finalArray[i] = tArray[i];
                                        index = i;
                                        iArray[i] = "0";
                                        tArray[i] = "0";
                                        break c;
                                    }
                                    else{
                                        textArea2.append(sta+" ");
                                        oldsta = sta;
                                        oldfta = fta;
                                        kk++;
                                        finalArray[i] = tArray[i];
                                        index = i;
                                        iArray[i] = "0";
                                        tArray[i] = "0";
                                        break c;    
                                    }
                                } 
                                else{
                                    loop:
                                    for(int ii = 0; ii < kk; ii++)
                                    {
                                        if(fta.equals(oldfta))
                                        {
                                            textArea1.append(finalArray[index]+"\n");
                                            textArea2.append("\n"+tArray[i]+" ");
                                            oldsta = tArray[i];
                                            finalArray[i] = tArray[i];
                                            index = i;
                                            iArray[i] = "0";
                                            tArray[i] = "0";
                                            break loop;
                                        }   
                                    }
                                    }
                            }
                        }
                        textArea1.append(finalArray[index]+"\n");

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,004
    Blog Entries
    7
    Rep Power
    20

    Default Re: show elements in correct order

    Quote Originally Posted by Shien View Post
    So I have two arrays with values iArray {A,C,E,B,H,C,B,F,D} and tArray{M,D,F,E,M,H,C,A,K}
    My code shows results like this (if I choose letter B):
    E,F
    C,D
    H,M

    But it shoud be like this:

    E,F,A,M
    C,D,K
    H,M
    Why should it be like that? Can you tell us the reason why?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: show elements in correct order

    I am writing program wich would show a path from letter you entered to the point it ends. So that is why there is 2 arrays: if array and then array;
    It's kinda similar to a tree. you go from a top down. In example above I showed how it should be.

    Sorry I am a bit rusty on my english so I can't explain this very well :/

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,004
    Blog Entries
    7
    Rep Power
    20

    Default Re: show elements in correct order

    I'd iterate over the iArray, trying to find a wanted symbol and I'd recurse over both arrays to find and print the nodes on a current 'branch'.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: show elements in correct order

    Your code is very hard to follow written like this. if you compartmentalised it that would make it easier to follow. anyhow, i had a look at your code and i understand what you are trying to do.

    1. Given a letter in the first array, get the index of that letter in the second array
    Java Code:
    public int getPath(char c, char[] iArr, char[] tArr) {
        int index = Arrays.binarySearch(iArr, c);
        if (index>=0) return Arrays.binarySearch(tArr, c);
        return index;
    }
    2. Handle char not found
    Java Code:
    int index = getPath('b', iArr, tArr);
    if (index >= 0) {
        //continue
    } else {
        //end
    }
    3. Set up a recursive method to explore one path
    Java Code:
    public void explorePath(char c, char[] iArr, char[] tArr) {
        //find the char in iArr
        ...
        //if found, output the char and recurse
        ...
        System.out.print(newChar);
        explorePath(newChar, tArr, iArr);
        //if not found end the output
        System.out.println();
    }
    I dont know if my logic is exactly right to your need but hopefully you get the idea of what I'm trying to explain (in my defense i'm answering from my smartphone and this site isnt very mobile friendly).
    Last edited by ozzyman; 12-30-2011 at 05:22 PM.

Similar Threads

  1. Replies: 1
    Last Post: 11-13-2011, 06:23 PM
  2. JList doesn't show the elements
    By mrbeast87 in forum AWT / Swing
    Replies: 2
    Last Post: 04-20-2011, 06:21 PM
  3. Replies: 3
    Last Post: 05-27-2010, 10:54 AM
  4. How to place the GUI components in correct order
    By impact in forum New To Java
    Replies: 2
    Last Post: 05-04-2008, 06:41 AM
  5. netbeans 6.0 not show commpunent or show blank page
    By fahimaamir in forum NetBeans
    Replies: 1
    Last Post: 01-26-2008, 06:20 AM

Posting Permissions

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