0

The code below shows my progress, but I cannot print the numbers that were entered. I don't know where to put println("you entered the following numbers") in the loop so that it'll show up when the loop stops.

import java.util.Scanner;

public class aufgabe5 {
    public static void main(String[] args) {

        int x;
        Scanner input = new Scanner(System.in);
        System.out.println("How much numbers do you want to enter?");
        x = input.nextInt();
        int j = 1;
        Scanner scanner = new Scanner(System.in);
        int[] numbers = new int[x];
        for (int i = 0; i < numbers.length; i++) {
            System.out.println("Enter the " + j++ + ". number:");
            numbers[i] = scanner.nextInt();

        }
        System.out.println("You entered following numbers");
        System.out.println(x);
    }
}
Justin
  • 6,611
  • 3
  • 36
  • 57
Saboteur
  • 97
  • 1
  • 1
  • 6
  • so i have to print out the array i... – Saboteur Nov 12 '14 at 17:56
  • 1
    possible duplicate of [What's the simplest way to print an array?](http://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-an-array) – BackSlash Nov 12 '14 at 17:56
  • Just loop back from 0 to numbers.length on your array, calling System.out.println(x) each time. – NSimon Nov 12 '14 at 17:59
  • hmm the problem is when a user enters for example 2 numbers 2 and 5...this 2 numbers will be saved in the numbers[i] array. i don't know how i can get acess tho this array... to print it out where the "sout "x" " is now – Saboteur Nov 12 '14 at 18:01

2 Answers2

1

Change x like

System.out.println(x);

to Arrays.toString(int[]) like

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

Edit

To print the array reversed,

String str = Arrays.toString(numbers).replace(", ", " ,");
str = str.substring(1, str.length() - 1);
System.out.println(new StringBuilder(str).reverse().insert(0, "[")
            .append("]"));
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

Intialize the String before the loop, then keep adding the values to it. After the loop finishes you can print the whole thing.

String someVar="You entered following numbers: ";
for(int 1=0;i<array.length;i++)
{
    someVar=someVar+numbers[i]+",";
}
System.out.println(someVar);
Bart Hofma
  • 644
  • 5
  • 19
  • Although the code seems pretty clear, it's good form to add a bit of explanatory text to an answer like this. – Caleb Nov 12 '14 at 18:34