Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    EASY problem with arraylist indexoutofboundsexeption

    Code:
    ArrayList<String> lastname2 = new ArrayList();
            //int x = lastname.size() - 1;
            int x = lastname.size();
            //System.out.print(x);
            do
            {
            lastname2.add(lastname.get(x));
            x = x-1;
            }while (x != -1);
            Collections.sort(lastname, String.CASE_INSENSITIVE_ORDER);
            String moreinput = JOptionPane.showInputDialog(null, counter + " people voted.\n\rTheir last names:\n\r " + lastname + "\n\rEnter the last name of whoever you want to look up: ");
            int finalindex = lastname2.indexOf(moreinput);
            JOptionPane.showMessageDialog(null, "Their information:\n\r" + firstname.get(finalindex) + lastname2.get(finalindex) + "\n\r" + address.get(finalindex) + "\n\r" + city.get(finalindex) + ", " + state.get(finalindex) + "\n\r" + zipcode.get(finalindex));
    This part of a voting poll program I'm making is supposed to duplicate the arraylist "lastname" and call the new arraylist "lastname2," then sort lastname arraylist alphabetically, while letting you look up the information of any voter by referencing lastname2. The two lines commented out are some variations I've been experimenting with (the println(x) is just to see if x even gets a value set (spoiler: it doesnt....))
    I'm very new to java, so any help solving this would be appreciated.
    --
    I have a feeling the solution is very simple and I'm just missing something, or if somebody has a suggestion as to a better way to code this instead of a DoWhile loop....
    Last edited by ezra; 11-16-2012 at 02:14 AM.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,565
    Thanks
    80
    Thanked 4,620 Times in 4,583 Posts
    This is the JavaSCRIPT forum.

    Your question is about JAVA.

    About the only thing the two languages have in common are the first 4 letters of their names.

    Try posting in the right forum.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,565
    Thanks
    80
    Thanked 4,620 Times in 4,583 Posts
    But even so your problem is obvious.
    Code:
        int x = lastname.size();
         do
        {
            lastname2.add(lastname.get(x));
    If the ArrayList conteins 7 elements, they will be numbered 0,1,2,3,4,5,6.

    So there WILL NOT BE any array element numbered 7.

    So your code fails on ther very first get(x) where x is 7.

    Now try
    Code:
        int x = lastname.size() - 1;
    Or, even simpler,
    Code:
            for ( int x = lastname.size() - 1; x >= 0; --x )
            {
                lastname2.add(lastname.get(x));
            }
    Or if you don't like for (I do!) then:
    Code:
        int x = lastname.size() ;
        while ( --x >= 0  )
        {
            lastname2.add( lastname.get( x );
        }
    Last edited by Old Pedant; 11-16-2012 at 02:29 AM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i thought of that also, as you can see with one of the lines commented out... I tried
    int x = lastname.size() - 1;
    same thing...
    also i just tried all 3 of the options you kindly provided, but none worked.

    also, i posted that while i was on my phone in a rush, sorry it's in the wrong place.

    should i post my whole code or something? it looks like the problem isn't with this area.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,565
    Thanks
    80
    Thanked 4,620 Times in 4,583 Posts
    Hopefully a moderator will move the thread.

    In the meantime: DEBUG DEBUG DEBUG.

    Use a debugger to inspect what is happening.

    Or at least put some priintln debug in there:
    Code:
        int x = lastname.size() ;
        System.out.println("x initialized to " + x);
        while ( --x >= 0  )
        {
            System.out.println( "x=" + x + ", get(x) gives " + lastname.get(x) );
            lastname2.add( lastname.get( x );
        }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    well i found the problem... the variables that were added to "lastname" were.... never added. but i don't really know how to add them... the code i have is about 50 lines though, are you allowed to post that long? lol

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,565
    Thanks
    80
    Thanked 4,620 Times in 4,583 Posts
    50 lines better be okay. I post a lot longer than that sometimes.

    But now that you know the problem isn't in the code shown here, start a new thread with the appropriate question and code in the JAVA forum, instead.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    As mentioned, when copying out of a list you should use a for or a while, not a do/while. Do while's indicate that its guaranteed to perform the first action, whilst your list is currently empty so you cannot retrieve the first item. You can use a try/catch of course.
    It may be a lot easier to simply invoke the .clone() method on the primary list though and use that copy. Its shallow clone, but if its just for sorting for whatever reason than that is fine.

    If the problem is adding to the collection, post that code.


  •  

    Posting Permissions

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