Search code examples
javaint

How to read 3 Integer values in single line of input in Java?


How do you scan 3 variable in one line its like i have 3 int variable named ( x , y and z) I want to input the three of them in a single line

i can input like this 7 21 35 < single line

        int x = 7;
        int y = 21;
        int z = 35;

        x = sc.nextInt();
        y = sc.nextInt();
        z = sc.nextInt();

        System.out.printf("%2d, %2d, %2d\n", x, y, z);

I have found something in C++ their code is like this ( scanf ("%lf %lf %lf", &x, &y, &z); )


Solution

  • Here is small example for you

    public static void main(String[] args) throws IOException { 
       BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
    
       String[] input = new String[3]; 
       int x; 
       int y;
       int z;
    
       System.out.print("Please enter Three integers: "); 
       input = in.readLine().split(" "); 
    
       x = Integer.parseInt(input[0]); 
       y = Integer.parseInt(input[1]);
       z = Integer.parseInt(input[2]); 
    
       System.out.println("You input: " + x + ", " + y + " and " + z); 
    }