+ 1
Input multi int numbers in one line
I want to let the user enter multi numbers in one line, the Scanner let me just enter them one by one using ( next.Datatype(); )
6 Respuestas
+ 9
Use scannerName.nextInt();
As long as each number is separated by spaces it will recognize them individually in order.
edit:
@Tashi
Yes, but you don't need 2 different inputs for nextInt() to work on code playground anyway.
+ 14
@Rrestoring Ahhh, you think he asked in general and not for the code playground ^^ OK, now he will get it to work anyway.
+ 13
You could read them as string, then parse the string to an char array and then parse the elements to integer.
+ 3
Why not use BufferedReader?
+ 3
Here is the example
import java.util.Scanner;
import java.io.*;
public class Program
{
public static void main(String[] args) {
System.out.println("Enter age salary");
Scanner s = new Scanner(System.in);
int age = s.nextInt();
int sal = s.nextInt();
System.out.print("Age is "+age+" salary is "+sal );
}
}
https://code.sololearn.com/cl1ga8tIeDBG/?ref=app
+ 3
Thank you all of guys, it worked for me using spaces.