Jay da science noob
Jay da science noob

Reputation: 23

Java basic integer array

I can't figure out why my outcome is not:

Your first array element is: 0
1
2
3
4
5
6

(the expected output)

My code:

public class day7 {
    public static void main(String[] args){
        int arrayZ[] = new int[7];

        arrayZ[0] = 0;
        System.out.println("Your first array element is: " + arrayZ[0]);

        int i = 1;
        while (i <= 6){
            arrayZ[i] = i;
            i++;
        System.out.println(arrayZ[i]);
        }
    System.out.println("Each entry of the array is: " + arrayZ[0] + " " + arrayZ[1] + " " + arrayZ[2] + " " + arrayZ[3] + " " + arrayZ[4] + " " + arrayZ[5] + " " + arrayZ[6]);
    System.out.println("And, the sum of all array elements are: " + arrayZ[0] + arrayZ[1] + arrayZ[2] + arrayZ[3] + arrayZ[4] + arrayZ[5] + arrayZ[6]);
    }
}

Outcome/actual output:

Your first array element is: 0
0
0
0
0
0

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7 at day7.main(day7.java:14)

Upvotes: 1

Views: 130

Answers (5)

ThilinaMD
ThilinaMD

Reputation: 375

change while loop code as follows

while (i <= 6){
    arrayZ[i] = i;
    System.out.println(arrayZ[i]);
    i++;
}

Upvotes: -1

user7088977
user7088977

Reputation:

  • just switch the lines between i++ and System.out.println(arrayZ[i]);

  • and if you want calculate the sum just add them in a new variable or use ();

    public static void main(String[] args) {
    int arrayZ[] = new int[7];
    
    arrayZ[0] = 0;
    System.out.println("Your first array element is: " + arrayZ[0]);
    
    int i = 1;
    while (i <= 6) {
        arrayZ[i] = i;
        System.out.println(arrayZ[i]);
        i++;
    }
    System.out.println("Each entry of the array is: " + arrayZ[0] + " " + arrayZ[1] + " " + arrayZ[2] + " " + arrayZ[3] + " " + arrayZ[4] + " " + arrayZ[5] + " " + arrayZ[6]);
    System.out.println("And, the sum of all array elements are: " + (arrayZ[0] + arrayZ[1] + arrayZ[2] + arrayZ[3] + arrayZ[4] + arrayZ[5] + arrayZ[6]));
    

    }

Upvotes: 1

akhil_mittal
akhil_mittal

Reputation: 24197

Your code is correct only problem is in order of incrementing and printing.

public static void main(String[] args) {
    int arrayZ[] = new int[7];

    arrayZ[0] = 0;
    System.out.println("Your first array element is: " + arrayZ[0]);

    int i = 1;
    while (i <= 6)  {
        arrayZ[i] = i;
        System.out.println(arrayZ[i]);
        i++;
    }
    System.out.println("Each entry of the array is: " + arrayZ[0] + " " + arrayZ[1] + " " + arrayZ[2] + " " + arrayZ[3] + " " + arrayZ[4] + " " + arrayZ[5] + " " + arrayZ[6]);
    System.out.println("And, the sum of all array elements are: " + (arrayZ[0] + arrayZ[1] + arrayZ[2] + arrayZ[3] + arrayZ[4] + arrayZ[5] + arrayZ[6]));
}

Also you need to sum the items first so need to use brackets and then using + with String will convert other variable to String and will show the concatenated result. The output is:

Your first array element is: 0
1
2
3
4
5
6
Each entry of the array is: 0 1 2 3 4 5 6
And, the sum of all array elements are: 21

If you have fixed bounds for your loop counter (in your case they are 1 to 6) then it is better to make use of for loop.

Upvotes: 2

Raphael Amoedo
Raphael Amoedo

Reputation: 4475

Change to this:

while (i <= 6){
    arrayZ[i] = i;
    System.out.println(arrayZ[i]);
    i++;
}

Because the way you were using it, it tries to print arrayZ[7], that not exists.

Upvotes: 1

DoubleMa
DoubleMa

Reputation: 368

just switch the lines between i++ and System.out.println(arrayZ[i]);

and if you want calculate the sum just add them in a new variable or use ();

public static void main(String[] args) {
    int arrayZ[] = new int[7];

    arrayZ[0] = 0;
    System.out.println("Your first array element is: " + arrayZ[0]);

    int i = 1;
    while (i <= 6) {
        arrayZ[i] = i;
        System.out.println(arrayZ[i]);
        i++;
    }
    System.out.println("Each entry of the array is: " + arrayZ[0] + " " + arrayZ[1] + " " + arrayZ[2] + " " + arrayZ[3] + " " + arrayZ[4] + " " + arrayZ[5] + " " + arrayZ[6]);
    System.out.println("And, the sum of all array elements are: " + (arrayZ[0] + arrayZ[1] + arrayZ[2] + arrayZ[3] + arrayZ[4] + arrayZ[5] + arrayZ[6]));
}

Upvotes: 1

Related Questions