-3

How can I input multiple times in one line?

public class Awit {

    static void numbers(){

         Scanner user = new Scanner(System.in);
         int arraylist[] = new int [5];
        
        System.out.println("Enter five integers:");
        System.out.print("");

        for(int i = 0; i<=4; i++){
        arraylist[i] = user.nextInt();
        }

 public static void main (String []args){

 numbers();

} 
}

Output:

Enter five integers:
1
2
3
4
5

I want the output to be like this

1 2 3 4 5
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
  • 1
    Does this answer your question? [How to read multiple Integer values from a single line of input in Java?](https://stackoverflow.com/questions/23506429/how-to-read-multiple-integer-values-from-a-single-line-of-input-in-java) – devordem Sep 27 '21 at 07:22

1 Answers1

0

The program that you have written will read multiple numbers from a line ... if you give it multiple numbers on a line.

$ java Awit.java
Enter five integers:
1 2 3 4 5
$ 

The only issues with the code you have written are:

  • it is missing an import for Scanner, and
  • there is a } missing at the end of your numbers method.

Those problems will give compilation errors. Assuming that you correct them, you will not get any output (apart from the prompt to enter some numbers) because your program doesn't print the array(!)

If you want to know how to print the numbers, read the following:

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216