Reputation: 41
i am trying to printout the number the user entered, but i got an error. Here's my code:
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int size=0,score=0;
int [] a=new int[size];
int len=a.length;
do
{
System.out.print("Please enter a number between 1 to 5: ");
size=input.nextInt();
}
while ((size<1) || (size>5));
for (int i=1;i<=size;i++)
{
do
{
System.out.print("Enter your "+i+" score (1-100):");
score=input.nextInt();
}
while((score<1) || (score>100));
}
for (int i=1;i<=size;i++)
System.out.println(a[i]+ " ");
}
}
Here's my output and the error: Please enter a number between 1 to 5: 2 Enter your 1 score (1-100):54 Enter your 2 score (1-100):64 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at Week08.BigArray2.main(BigArray2.java:27)
Upvotes: 0
Views: 49
Reputation: 926
Please have a look at your 4th line of code int [] a=new int[size];
, size=0 at this time and you have initialized the integer array "a" with size 0 but you are trying to iterate array "a" at last with code for (int i=1;i<=size;i++)
System.out.println(a[i]+ " ");
}
which is causing the array index out of bound exception.
Move below lines of code after first do while loop-:
int[] a = new int[size];
int len = a.length;
And now correct your last for loop as below-:
for (int i = 0; i < size; i++)
System.out.println(a[i] + " ");
}
Index of array starts at 0 and ends at size-1.
Upvotes: 0
Reputation: 2912
Few issues with you code:
Here it is the modified working program:
import java.util.Scanner;
public class ArrayPrint {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int size = 0, score = 0;
int[] a;
do {
System.out.print("Please enter a number between 1 to 5: ");
size = input.nextInt();
} while ((size < 1) || (size > 5));
//Allocating space to array
a = new int[size];
int len = a.length;
for (int i = 0; i <size; i++) {
do {
System.out.print("Enter your " + i + " score (1-100):");
score = input.nextInt();
a[i] = score;
} while ((score < 1) || (score > 100));
}
for (int i = 0; i <size; i++)
System.out.println(a[i] + " ");
}
}
Output
Please enter a number between 1 to 5: 2
Enter your 0 score (1-100):22
Enter your 1 score (1-100):11
22
11
Upvotes: 0
Reputation: 12817
There are four mistakes in your code
Scanner not closed
Scanner input = new Scanner(System.in);
int size = 0, score = 0;
do {
System.out.print("Please enter a number between 1 to 5: ");
size = input.nextInt();
} while ((size < 1) || (size > 5));
int[] a = new int[size]; //1
for (int i = 0; i < size; i++) {
do {
System.out.print("Enter your " + (i + 1) + " score (1-100):");
score = input.nextInt();
a[i] = score; //2
} while ((score < 1) || (score > 100));
}
for (int i = 0; i < size; i++) //3
System.out.print(a[i] + " ");
input.close(); //4
output
Please enter a number between 1 to 5: 4
Enter your 1 score (1-100):99
Enter your 2 score (1-100):98
Enter your 3 score (1-100):97
Enter your 4 score (1-100):96
99 98 97 96
Upvotes: 1