• Register
0 votes
20 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,060 points

Please log in or register to answer this question.

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,950 points

Related questions

1 vote
1 answer 21 views
21 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, 2020 morrisBson 3.2k points
0 votes
1 answer 99 views
99 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 1K views
1K 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 6 views
0 votes
1 answer 2 views
2 views
Solve this problem, please.
asked 1 day ago TeamScript 10.3k points
0 votes
1 answer 6 views
6 views
Thank you in advance for any help?
asked Jan 5 TeamScript 10.3k points
0 votes
1 answer 15 views
15 views
Solve this problem.
asked Jan 1 TeamScript 10.3k points
0 votes
1 answer 36 views
36 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, 2020 mphil 2.3k points
0 votes
1 answer 16 views
16 views
Problem: I am a novice Java programmer who started learning to program in Java just a few weeks ago. I got an assignment from my tutor in which I have to write a program that prints the sum of the elements of a given array. The output of the program should be ... } I am allowed to take help from the Internet so asking for help from you guys. Can anybody please provide me with a solution? Thanks!
asked Dec 29, 2020 Code Learner 5.5k points
0 votes
1 answer 8 views
8 views
Problem: The easiest way to create an array of structures is to use the REPLICATE function. The first parameter to REPLICATE is a reference to the structure of each element. Using the example in and assuming the STAR structure has been defined, an array containing 100 elements of the structure is created with the following statement: cat = REPLICATE({star}, 100)
asked Dec 5, 2020 Mashhoodch 6.1k points