1

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)

3
  • call i++; after System.out.println(arrayZ[i]); Commented May 9, 2015 at 3:20
  • Step through the code in the debugger (e.g. use IntelliJ IDEA) and you'll see where it goes wrong. Commented May 9, 2015 at 3:20
  • When you're using while looping which relies on an inc/decrement sentinel value (i.e.; i in your case) a for loop is generally the more concise option. for (int i = 0; i <= 6; ++i) { ... }. Commented May 9, 2015 at 3:28

5 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much. Your comment is crystal clear and very helpful.
You just have to replace by i++ to System.out line ...because its array will bound on last index by i++.
1

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]));
}

Comments

1

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.

1 Comment

That was what I thought. :3
1
  • 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]));
    

    }

Comments

-1

change while loop code as follows

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

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.