Input multi int numbers in one line | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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(); )

10th May 2017, 8:31 PM
Mohamed Yassine Laggoun
Mohamed Yassine Laggoun - avatar
6 Answers
+ 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.
10th May 2017, 8:45 PM
Rrestoring faith
Rrestoring faith - avatar
+ 14
@Rrestoring Ahhh, you think he asked in general and not for the code playground ^^ OK, now he will get it to work anyway.
10th May 2017, 8:48 PM
Tashi N
Tashi N - avatar
+ 13
You could read them as string, then parse the string to an char array and then parse the elements to integer.
10th May 2017, 8:35 PM
Tashi N
Tashi N - avatar
+ 3
Why not use BufferedReader?
10th May 2017, 8:32 PM
Prathamesh Jadhav
Prathamesh Jadhav - avatar
+ 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
10th May 2017, 8:55 PM
Prathamesh Jadhav
Prathamesh Jadhav - avatar
+ 3
Thank you all of guys, it worked for me using spaces.
11th May 2017, 12:50 AM
Mohamed Yassine Laggoun
Mohamed Yassine Laggoun - avatar