Duy Khang Dao
Duy Khang Dao

Reputation: 41

Print out array user entered

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

Answers (3)

Ashish Kumar
Ashish Kumar

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

Khuzi
Khuzi

Reputation: 2912

Few issues with you code:

  1. Array declared with zero size. In java, size of array is declared at the time of initialization and it can altered later.
  2. You never stored score into an array. Hence array was empty.
  3. Always start loop index with zero in java while iterating array.

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

Saravana
Saravana

Reputation: 12817

There are four mistakes in your code

  1. array initialized with zero size
  2. user input not stored in the array
  3. iterating from index 1 but array starts with 0
  4. 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

Related Questions