• Register
0 votes
18 views

I wrote some code in java:

  public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter size of array:");
		int size = sc.nextInt();
		int[] intArray = new int[size];
		for (int i = 0; i < size; i++) {
			System.out.println("Please enter int value at index " + i + ":");
			intArray[i] = sc.nextInt();
		}
		System.out.println("Enter array index to get the value:");
		int index = sc.nextInt();
		sc.close();

		System.out.println("Value at " + index + " = " + intArray[index]);

But it gives me error:

 Enter size of int array:
     3
     Please enter int value at index 0:
     1
    Please enter int value at index 1:
     2
    Please enter int value at index 2:
    3
   Enter array index to get the value:
    4
   Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
   at com.journaldev.exceptions.ArrayIndexOutOfBoundsExceptionExample.main(ArrayIndexOutOfBoundsExceptionExample.java:23)

What is the problem in my code and why am I getting this kind of error?

10 7 2
6,050 points

1 Answer

0 votes
java.lang.ArrayIndexOutOfBoundsException is a runtime exception, so it’s a unchecked exception and don’t need to be thrown explicitly from method.The code snippet below shows the small change in program while taking user input to access the element, if value is passed invalid then a warning message is shown to pass the valid value.
   boolean exit = false;
     while (!exit) {
	System.out.println("Enter array index to get the value:");
	int index = sc.nextInt();
	if (index < 0 || index >= size) {
		System.out.println("Valid index range is from 0 to " + (size - 1));
	} else {
		System.out.println("Value at " + index + " = " + intArray[index]);
		exit = true; //to terminate the program
		sc.close(); //close resources
	}
     }
So this is how we avoid getting ArrayIndexOutOfBoundsException in our program
11 6 4
34,930 points

Related questions

1 vote
1 answer 16 views
16 views
Problem : I have very recently started to learn programming in Java. I am trying to execute my java code but I am facing the below error. &ldquo;Exception in thread "main" java.lang.arrayindexoutofboundsexception:&rdquo; I am unable to understand above error. Please find below my code: ... according to my debugger*/ myodd[j] = input[i]; j++; } return myodd; } Please help me in fixing above error.
asked Mar 30 morrisBson 3.2k points
0 votes
1 answer 92 views
92 views
Problem : I am a beginner to Java programming. While executing my program i got below error exception in thread "main" java.lang.arrayindexoutofboundsexception: 10
asked Oct 23, 2019 peterlaw 6.9k points
0 votes
1 answer 743 views
743 views
Problem : I am getting bellow error can only iterate over an array or an instance of java.lang.iterable
asked Nov 1, 2019 peterlaw 6.9k points
0 votes
1 answer 25 views
25 views
Problem : Currently I am learning Java. I have written code in Java but it gives me below error &ldquo;Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at Minesweeper.main(Minesweeper.java:5)&rdquo;. I am unable to understand above error. How can I fix above error.
asked Feb 22 mphil 2.3k points
0 votes
1 answer 2 views