+ 2
Its possible take multiple inputs from the user in a same line in java prompt?
12 Answers
+ 5
Yes, you can seperate them with a space (or some other delimiter) then use the appropriate Scanner methods.
+ 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);
}
}
+ 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.
+ 3
Either way you just need to add 3 more lines.
int a2 = sc.nextInt();
int b2 = sc.nextInt();
double d2 = sc.nextDouble();
+ 2
Please, can you show me an example?
+ 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
+ 1
Ok, i'll try. Thank you very much!
0
I tried to use the BufferedReader but does not work, It still input in diferent lines
0
Its two int values and a Double value
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
0
Its two int values and a Double value in one line, then the same three values in a Second line input
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();
}
}