Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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);
    }
}
share|improve this question
    
Loop the array to print all values. –  Subir Kumar Sao Oct 20 '13 at 17:54
    
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:55
add comment

4 Answers

The easiest way is

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

share|improve this answer
add comment

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

    for(int no : array ) {
        System.out.println(no);
    }
share|improve this answer
add comment

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);
}
share|improve this answer
add comment
for (int i = 0 ; i < array.length; i++){
   System.out.println("Value "+i+"="+array[i]);
}

thats all my friend :)

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.