Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-16-2012, 12:52 AM   PM User | #1
ezra
New to the CF scene

 
Join Date: Nov 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
ezra is an unknown quantity at this point
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 01:14 AM..
ezra is offline   Reply With Quote
Old 11-16-2012, 01:22 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 11-16-2012, 01:26 AM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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 );
    }
__________________
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.

Last edited by Old Pedant; 11-16-2012 at 01:29 AM..
Old Pedant is offline   Reply With Quote
Old 11-16-2012, 02:03 AM   PM User | #4
ezra
New to the CF scene

 
Join Date: Nov 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
ezra is an unknown quantity at this point
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.
ezra is offline   Reply With Quote
Old 11-16-2012, 02:30 AM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 11-16-2012, 02:42 AM   PM User | #6
ezra
New to the CF scene

 
Join Date: Nov 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
ezra is an unknown quantity at this point
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
ezra is offline   Reply With Quote
Old 11-16-2012, 02:49 AM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 11-16-2012, 02:21 PM   PM User | #8
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,648
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:33 PM.


Advertisement
Log in to turn off these ads.