madnomad10011
madnomad10011

Reputation: 341

array based on user input, error?

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]);

        }
}

Upvotes: 1

Views: 123

Answers (4)

Reimeus
Reimeus

Reputation: 159784

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.

Upvotes: 2

Manish Doshi
Manish Doshi

Reputation: 1193

Remove semicolon from for loop.

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

Upvotes: 1

Aleks G
Aleks G

Reputation: 57316

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]);
}

Upvotes: 1

JREN
JREN

Reputation: 3630

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++) {}

Upvotes: 2

Related Questions