+ 2

Its possible take multiple inputs from the user in a same line in java prompt?

24th Sep 2017, 10:52 PM
Rafael Leonardo De Lima
Rafael Leonardo De Lima - avatar
12 Answers
+ 5
Yes, you can seperate them with a space (or some other delimiter) then use the appropriate Scanner methods.
24th Sep 2017, 11:03 PM
ChaoticDawg
ChaoticDawg - avatar
+ 4
import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // example input // 1 2 3.5 int a = sc.nextInt(); int b = sc.nextInt(); double d = sc.nextDouble(); System.out.println(a + " " + b + " " + d); } }
25th Sep 2017, 12:09 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
So you want 2 ints a double 2 ints and a double all on one line? or 2 ints and a double on one line and then another 2 ints and a double on a second line? Either way you can modify it to work for your code.
25th Sep 2017, 12:21 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
Either way you just need to add 3 more lines. int a2 = sc.nextInt(); int b2 = sc.nextInt(); double d2 = sc.nextDouble();
25th Sep 2017, 12:26 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Please, can you show me an example?
24th Sep 2017, 11:30 PM
Rafael Leonardo De Lima
Rafael Leonardo De Lima - avatar
+ 2
Sure what all are you trying to get as input types in one line? I.E. 5 ints or 1 string and 2 doubles etc etc
24th Sep 2017, 11:54 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Ok, i'll try. Thank you very much!
25th Sep 2017, 12:27 AM
Rafael Leonardo De Lima
Rafael Leonardo De Lima - avatar
0
I tried to use the BufferedReader but does not work, It still input in diferent lines
24th Sep 2017, 11:47 PM
Rafael Leonardo De Lima
Rafael Leonardo De Lima - avatar
0
Its two int values and a Double value
24th Sep 2017, 11:58 PM
Rafael Leonardo De Lima
Rafael Leonardo De Lima - avatar
0
It works, but the input is still in diferent lines. I trying to do like this: Input sample: First input in prompt: 1 2 3.4 Second input in prompt: 5 6 7.8
25th Sep 2017, 12:15 AM
Rafael Leonardo De Lima
Rafael Leonardo De Lima - avatar
0
Its two int values and a Double value in one line, then the same three values in a Second line input
25th Sep 2017, 12:23 AM
Rafael Leonardo De Lima
Rafael Leonardo De Lima - avatar
0
It's simple you can use a nested for: for (int i = 0; i < sizeMatrix; i++) { for (int j = 0; j < sizeMatrix; j++) { matrix[i][j] = input.nextInt(); } }
20th Mar 2020, 1:17 PM
Josue Villegas Miguel
Josue Villegas Miguel - avatar