-2

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); )

  • and what is wrong with what you have? – Scary Wombat Jun 08 '20 at 02:28
  • Does this answer your question? [Java reading multiple ints from a single line](https://stackoverflow.com/questions/23506429/java-reading-multiple-ints-from-a-single-line) –  Jun 08 '20 at 02:29
  • What i have is i can only input the integers in each line. I would like to input the 3 integers in a single line using spaces.. – Joshua Arrogante Jun 08 '20 at 02:30

1 Answers1

0

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); 
} 
Kunal Vohra
  • 2,703
  • 2
  • 15
  • 33