1

i went over this code several time and i really cant understand why am getting the Error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
at qrt2.practice.main(practice.java:34)

am using Eclips standard SDK

and the code is :

package qrt2;
import javax.swing.*;

public class practice {

        public static void main(String[] args) {

//creating JOption for the input

            String num1 = JOptionPane.showInputDialog("Enter the first number");
            int x = Integer.parseInt(num1);

            String num2 = JOptionPane.showInputDialog("Enter the first number");
            int y = Integer.parseInt(num2);

            String num3 = JOptionPane.showInputDialog("Enter the first number");
            int z = Integer.parseInt(num3);

            String num4 = JOptionPane.showInputDialog("Enter the first number");
            int v = Integer.parseInt(num4);

            String num5 = JOptionPane.showInputDialog("Enter the first number");
            int h = Integer.parseInt(num5);

            String num6 = JOptionPane.showInputDialog("Enter the first number");
            int k = Integer.parseInt(num6);


//creating array

            int [] ary = new int []{x,y,z,v,h,k};
            int counter;
            for( counter=0;counter<ary.length;counter++);
            System.out.println(counter + " " + ary[counter]);

        }
}

4 Answers 4

2

Remove the trailing semi colon from the for statement

for (counter = 0; counter < ary.length; counter++);
                                                  ^

By leaving the semi-colon in place counter equals ary.length once it reaches this statement

System.out.println(counter + " " + ary[counter]);

As arrays are zero based this will cause an ArrayIndexOutOfBoundsException

Unless the variable counter is required outside the loop, move it into the for statement initialization section like this

for (int counter=0; counter < ary.length; counter++) {
    System.out.println(counter + " " + ary[counter]);
}

This will make the ary[counter] expression show up immediately a compilation failure. Notice also the braces give clarity of scope.

Sign up to request clarification or add additional context in comments.

Comments

2
for(counter=0;counter<ary.length;counter++) {
    System.out.println(counter + " " + ary[counter]);
}

Remove the semicolon and replace it with a curly opening bracket. Then close the curly bracket after your output.

And if you want to apply best practise, you should also declare your counter variable inside your loop like this:

for(int counter = 0; counter < ary.length; counter++) {}

Comments

1

Your problem is in these lines:

for( counter=0;counter<ary.length;counter++);
System.out.println(counter + " " + ary[counter]);

Specicifically, the first of these completes the loop and the second prints a line - after the loop has completed and the counter incremented beyond the length of the array (thus breaking out of the loop). What you need is to print inside the loop, so these two lines should be:

for( counter=0;counter<ary.length;counter++)
    System.out.println(counter + " " + ary[counter]);

(note the removal of semicolon at the end of the first line). Or, to make it even more clear:

for( counter=0;counter<ary.length;counter++) {
    System.out.println(counter + " " + ary[counter]);
}

Comments

1

Remove semicolon from for loop.

for (counter = 0; counter < ary.length; counter++)
{
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.