11 Replies - 10227 Views - Last Post: 03 February 2012 - 08:26 AM Rate Topic: -----

#1 deprosun  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 307
  • Joined: 16-November 10

How is my Array List going "java.lang.IndexOutOfBoundsException

Posted 03 February 2012 - 07:55 AM

Am I mistaking any concept here?

import java.util.ArrayList;
import javax.swing.JOptionPane;

public class ExArrayList {

    ArrayList< Integer> grades = new ArrayList< Integer>();
    private int num=0;
    private boolean notnumber;

    protected void calc(int n) {
        for (int i = 0; i < n; i++) {
            do {
                try {
                    num = Integer.parseInt(JOptionPane.showInputDialog("Enter grade upto 25"));
                    notnumber = false;
                    grades.add(num);

                } catch (java.lang.NumberFormatException e) {
                    JOptionPane.showMessageDialog(null, "Type only numbers");
                    notnumber = true;
                }
            } while (notnumber == true);

        }
        average();
        JOptionPane.showMessageDialog(null, "Average is" + num);
        grades.size();


    }

    private void average() {
        num = 0;
        for (int c : grades) {
            num = num + grades.get(c);
        }

    }
}



Below is my main class
import javax.swing.JOptionPane;

public class ArrayListEx {

    public static void main(String args[]) {
        int num1 = 0;
        boolean notnumber;
        String number;

        ExArrayList al = new ExArrayList();

        do {
            try {
                number = JOptionPane.showInputDialog("How many grades do you want to Enter (Type a Number)");
                num1 = Integer.parseInt(number);
                notnumber = false;
            } catch (java.lang.NumberFormatException e) {
                JOptionPane.showMessageDialog(null, "Type a number!");
                notnumber = true;
            }
        } while (notnumber == true);
        al.calc(num1);


    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: How is my Array List going "java.lang.IndexOutOfBoundsException

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: How is my Array List going "java.lang.IndexOutOfBoundsException

Posted 03 February 2012 - 07:59 AM

Are you getting error(s)? Please post them exactly as they appear, copied and pasted.

Please ask more specific questions so that we don't have to guess what you want to know.
Was This Post Helpful? 1
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,377
  • Joined: 02-June 10

Re: How is my Array List going "java.lang.IndexOutOfBoundsException

Posted 03 February 2012 - 08:01 AM

On which line (of which block) does the error happen?
Was This Post Helpful? 1
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Chinga la migra
  • member icon


Reputation: 10720
  • View blog
  • Posts: 18,353
  • Joined: 19-March 11

Re: How is my Array List going "java.lang.IndexOutOfBoundsException

Posted 03 February 2012 - 08:09 AM

   private void average() {
        num = 0;
        for (int c : grades) {
            num = num + grades.get(c);
        }
    }



Pretty sure it's here. You have a list of numbers, let's say it's got {1, 4, 2 , 75, 0}

You take each of these numbers and you try do this:

         num = num + grades.get(c);


That is, you try to do this:

         num = num + grades.get(1);
         num = num + grades.get(4);
         num = num + grades.get(2);
         num = num + grades.get(75);  // boom!
         num = num + grades.get(0);



Clear?

Some bugs are relatively easy to track. Any time you have an index out of bounds, you just look for an access, so all I had to do was search for "get" and there it was. However, it's always a good idea to provide stack trace when you're asking for help. The more information you give, the more likely it is you'll get useful help.

This post has been edited by jon.kiparsky: 03 February 2012 - 08:12 AM

Was This Post Helpful? 4
  • +
  • -

#5 deprosun  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 307
  • Joined: 16-November 10

Re: How is my Array List going "java.lang.IndexOutOfBoundsException

Posted 03 February 2012 - 08:11 AM

Sorry if i wasn't clear everybody!
I asked to input 5 ints in the array-list grades and it does. But for some reason this is the error i get after the for loop in ExArrayList class is executed.

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 5, Size: 5
	at java.util.ArrayList.RangeCheck(ArrayList.java:547)
	at java.util.ArrayList.get(ArrayList.java:322)
	at ExArrayList.average(ExArrayList.java:44)
	at ExArrayList.calc(ExArrayList.java:34)
	at ArrayListEx.main(ArrayListEx.java:32)
Java Result: 1

Was This Post Helpful? 0
  • +
  • -

#6 ianian112  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 119
  • View blog
  • Posts: 378
  • Joined: 28-November 09

Re: How is my Array List going "java.lang.IndexOutOfBoundsException

Posted 03 February 2012 - 08:14 AM

    private void average() {
        num = 0;
        for (int c : grades) {
            num = num + grades.get(c);
        }

    }


should be
    private void average() {
        num = 0;
        for (int c : grades) {
            num = num + c;
        }

    }


Was This Post Helpful? 1
  • +
  • -

#7 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1822
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: How is my Array List going "java.lang.IndexOutOfBoundsException

Posted 03 February 2012 - 08:15 AM

As said above, please be specific::
Look at this part here:
for (int c : grades) {
   num = num + grades.get(c);

You are using for each loop which gives you every element from the list, then you use the element as the index to your array list!!!! grades.get©
Oh very slow, jon has explained it already...

This post has been edited by smohd: 03 February 2012 - 08:18 AM

Was This Post Helpful? 0
  • +
  • -

#8 deprosun  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 307
  • Joined: 16-November 10

Re: How is my Array List going "java.lang.IndexOutOfBoundsException

Posted 03 February 2012 - 08:22 AM

Jon, i have tested that. It doesn't do what you're saying. It gets the element in the arraylist grades of c. It's got to to be something in the for loop coz when i changed
for(int i = 0; i<n; i++)
to
(int i = 0; i<=n; i++)
. Program doesn't crash but it takes it takes more than asked. For ex: you asked to input 5 numbers and it takes 6 (which i get).

You see what i am saying?

Ooooooooooooooooooooooh! I feel like a dumbass, well i am. I am still learning enhanced for loop. thanls everybody. Special thanks to ianian 112

Ooooooooooooooooooooooh! I feel like a dumbass, well i am. I am still learning enhanced for loop. thanls everybody. Special thanks to ianian112!! CHEERS IT WORKED!! :D
Was This Post Helpful? 0
  • +
  • -

#9 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1822
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: How is my Array List going "java.lang.IndexOutOfBoundsException

Posted 03 February 2012 - 08:23 AM

The problem is not with the add() method of the arrayList, it is with the get() method of it.
The add() method(with one parameter) does not throw any exception but the get() method throws exception if the index passed is greater than or equal to the size of the list
Was This Post Helpful? 1
  • +
  • -

#10 deprosun  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 307
  • Joined: 16-November 10

Re: How is my Array List going "java.lang.IndexOutOfBoundsException

Posted 03 February 2012 - 08:24 AM

View Postianian112, on 03 February 2012 - 08:14 AM, said:

    private void average() {
        num = 0;
        for (int c : grades) {
            num = num + grades.get(c);
        }

    }


should be
    private void average() {
        num = 0;
        for (int c : grades) {
            num = num + c;
        }

    }



Thanks man:) Still learning enhanced for loop
Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky  Icon User is offline

  • Chinga la migra
  • member icon


Reputation: 10720
  • View blog
  • Posts: 18,353
  • Joined: 19-March 11

Re: How is my Array List going "java.lang.IndexOutOfBoundsException

Posted 03 February 2012 - 08:25 AM

Quote

Jon, i have tested that. It doesn't do what you're saying.



private void average() {
    num = 0;
    for (int c : grades) {           // for each integer c in the list called grades
        num = num + grades.get(c);   // get the integer at index c of the list grades
    }

}



You're using the values in the list to index into the list, without doing any bounds checking.
Another way to write this:

for (int c : grades)
{
  if ( c  >= grades.size())
   {
      haltAndCatchFire();
   }
}



If you don't believe this is the cause of the error, put in some bounds checking:
private void average() {
    num = 0;
    for (int c : grades) {
        if (c < grades.size()) 
          num = num + grades.get(c);
        else
          System.out.println("Looks like Jon was right...");
    }
}

This post has been edited by jon.kiparsky: 03 February 2012 - 08:26 AM

Was This Post Helpful? 1
  • +
  • -

#12 deprosun  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 307
  • Joined: 16-November 10

Re: How is my Array List going "java.lang.IndexOutOfBoundsException

Posted 03 February 2012 - 08:26 AM

View Postjon.kiparsky, on 03 February 2012 - 08:25 AM, said:

private void average() {
    num = 0;
    for (int c : grades) {           // for each integer c in the list called grades
        num = num + grades.get(c);   // get the integer at index c of the list grades
    }

}



You're using the values in the list to index into the list, without doing any bounds checking.
Another way to write this:

for (int c : grades)
{
  if ( c  >= grades.size())
   {
      haltAndCatchFire();
   }
}



If you don't believe this is the cause of the error, put in some bounds checking:
private void average() {
    num = 0;
    for (int c : grades) {
        if (c < grades.size()) 
          num = num + grades.get(c);
        else
          System.out.println("Looks like Jon was right...");
    }
}


You were right thanks :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1