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
  • 118,400
  • 24
  • 176
  • 257
user1534779
  • 53
  • 1
  • 1
  • 6

4 Answers4

5

The easiest way is

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

AlexR
  • 111,884
  • 15
  • 126
  • 200
1

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

    for(int no : array ) {
        System.out.println(no);
    }
Aniket Thakur
  • 63,511
  • 37
  • 265
  • 281
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
  • 24,936
  • 10
  • 54
  • 63
0
for (int i = 0 ; i < array.length; i++){
   System.out.println("Value "+i+"="+array[i]);
}

thats all my friend :)

Alexis C.
  • 87,500
  • 20
  • 164
  • 172
s_bei
  • 11,949
  • 8
  • 50
  • 73
  • 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