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

3
  • and what is wrong with what you have? Commented Jun 8, 2020 at 2:28
  • Does this answer your question? Java reading multiple ints from a single line Commented Jun 8, 2020 at 2: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.. Commented Jun 8, 2020 at 2:30

1 Answer 1

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); 
} 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.