user963395
user963395

Reputation:

Reading integers from a line

I'm trying to read two integers on the same line.

Scanner a=new Scanner(System.in);
x=a.nextInt();
y=a.nextInt();

Now, if I input

3 4
3 4

x = 3 and y = 3. I even tried using a.useDelimiter("\\s") but it doesn't work.

Upvotes: 2

Views: 3553

Answers (1)

Mark Byers
Mark Byers

Reputation: 838116

There must be an error elsewhere in your code. It works fine for me.

import java.util.Scanner;

class Main
{
    public static void main(String[] args)
    {
        Scanner a = new Scanner(System.in);
        int x = a.nextInt();
        int y = a.nextInt();
        System.out.println("x = " + x + ", y = " + y);
    }
}

Input:

3 4
3 4

Output:

x = 3, y = 4

See it working online: ideone

Upvotes: 3

Related Questions