0

How do I print what is stored in the array in an easy way. I have been staring at this for the past 3 hours!

import java.util.Scanner;

public class December2012 {

    public static void main(String[] args) {

        int[] array = new int[100];
        int sum = 0;
        int i = 0;
        Scanner input = new Scanner(System.in);
        while (sum <= 100 && i < array.length) {
            System.out.print("Write in the " + (i + 1) + "th number: ");
            array[i] = input.nextInt();
            sum += array[i];
            i++;

        }
        System.out.print("You added: ");
        System.out.print(array[i] + " ");

        System.out.println("\nsum is " + sum);
    }
}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
user1534779
  • 51
  • 1
  • 2
  • 6

4 Answers4

5

The easiest way is

System.out.println(Arrays.toString(array));

AlexR
  • 114,158
  • 16
  • 130
  • 208
1

Just for the record you could also use the new for-each loop

    for(int no : array ) {
        System.out.println(no);
    }
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
0

you can do like this

for(int j= 0 ; j<array.length;j++) {
    System.out.println(array[j]);
}

or

for (int j: array) {
     System.out.println(j);
}
Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
0
for (int i = 0 ; i < array.length; i++){
   System.out.println("Value "+i+"="+array[i]);
}

thats all my friend :)

Alexis C.
  • 91,686
  • 21
  • 171
  • 177
nano_nano
  • 12,351
  • 8
  • 55
  • 83
  • Tried that but since I already am using i to store the array and if I try printing with j it prints 0 – user1534779 Oct 20 '13 at 17:59
  • The code given above will always sum to zero, because you allocate the array, but you never put any values in it. – AgilePro Oct 20 '13 at 18:11