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();
开发者_JS百科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.
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
精彩评论