Ellie Riggs
Ellie Riggs

Reputation: 13

Why get ArrayIndexOutOfBound Exception?

Modify the program below:

public class LenghtArray {

    public static void main(String[] args) {
        
        int[] ages = {16, 19, 30, 60, 21, 25, 2, 13};
        // Print all elements
        for (int count = 0; count < ages.length; count++) {
            System.out.print(ages[count] + " ");
        }

        // Sum of all ages
        System.out.println(" ");
        int total = 0;
        for (int count = 0; count < ages.length; count++) {
            total = total + ages[count];
        }
        System.out.print("Total :" + total + " ");
    }
}

Here down is my expected output:

Input length of an array: 4

age[0]: 65

age[1]: 10

age[2]: 60

age[3]: 18


Display all elements in an array: 65, 10, 60, 18

Total: 153

So far here's what I have, I don't know what's wrong with it. My professor said you just need to add 2 string of lines. I keep on adding more

public static void main(String[] args) {

    Scanner inp = new Scanner(System.in);
    int[] ages = {0};

    System.out.println("Input length of an array:");
    int number = inp.nextInt();
    for (int count = 0; count < number; count++) {
        System.out.println("age[" + count + "]: ");
        ages[count] = inp.nextInt();
    }

    // Print all elements
    for (int count = 0; count < ages.length; count++) {
        System.out.print(ages[count] + " ");
    }

    // Sum of all ages
    System.out.println(" ");
    int total = 0;
    for (int count = 0; count < ages.length; count++) {
        total = total + ages[count];
    }
    System.out.print("Total :" + total + " ");
}

Upvotes: 0

Views: 94

Answers (2)

cyberbrain
cyberbrain

Reputation: 5075

You need to change place and style of the declaration and creation of the age array:

  • declare it after you know it's desired length
  • create it with the desired length:
int number = inp.nextInt();
int[] ages = new int[number];

Rest of your code looks good. You might want to improve it a bit by printing the texts before the inputs (like "Input length of an array:") only with System.out.print instead of System.out.println - this will make the input on the same row as the text before.

In contrary you should use the println for the total output.

Upvotes: 1

Oleg Cherednik
Oleg Cherednik

Reputation: 18245

I would recommend first of all making decomposition of the task into smaller element steps. Then implement each step and check the result. In this case, you can easier find a bug in the program.

P.S. Your teacher could give a + sign seeing this

public class LengthArray {

    public static void main(String... args) {
        int[] ages = createArray();
        printArray(ages);
        printTotal(ages);
    }

    private static int[] createArray() {
        Scanner scan = new Scanner(System.in);

        System.out.print("Input length of an array: ");
        int[] ages = new int[scan.nextInt()];

        for (int i = 0; i < ages.length; i++) {
            System.out.format("age[%d]: ", i);
            ages[i] = scan.nextInt();
        }

        return ages;
    }

    private static void printArray(int... ages) {
        System.out.println("Display all elements in an array: " + Arrays.toString(ages));
    }

    private static void printTotal(int... ages) {
        System.out.println("Total: " + calcTotal(ages));
    }

    private static int calcTotal(int... ages) {
        int total = 0;

        for (int age : ages)
            total += age;

        return total;
    }

}

Output:

Input length of an array: 4
age[0]: 65
age[1]: 10
age[2]: 60
age[3]: 18
Display all elements in an array: [65, 10, 60, 18]
Total: 153

Upvotes: 1

Related Questions