0

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 + " ");
}
3
  • 1
    int [] ages= {}; is an array with length 0. You can't access any of its indices (and you should be getting an IndexOutOfBoundsException) Commented Feb 28, 2022 at 10:50
  • And also you don't need second array in array2 class. Commented Feb 28, 2022 at 10:54
  • @user16320675 I think only "age[0]:" is to be written by the program, the "65" is the user input Commented Feb 28, 2022 at 11:20

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

actually I think the split into such obvious and super-tiny methods makes it harder to read and understand - and you need to know more concepts of Java to understand it, like methods and varargs. It's also possible that you get a - for that if your teacher finds out that you copied your answer from StackOverflow because it's so super-fancy ;-)
maybe teacher can give a - sign if seeing this post as solution... :-|
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.