I have one input having three different values. How can i scan three values form a single scanner

Next t lines contain three space-separated integers N, B1, B2 and where N is the number of sides in the polygon and B1, B2 denote the vertices that are colored black.

How can I extract numbers from single input using only one scanner object?

2

2 Answers

You can use String[] input = scanner.nextLine().split(" ") which will return a String array of the values that are entered on a single line. For example, if 1 2 3 is entered from the console, in input you will have ["1", "2", "3"].

0

Since you are using Scanner, a simple Scanner.nextInt() will suffice to read integers as long as they are separated by whitespace as mentioned in your problem statement.

A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.

For instance:

int n1,n2; Scanner sc = new Scanner(System.in); n1=sc.nextInt(); n2=sc.nextInt(); 

If the input is 20 40, n1 will store 20 and n2 will store 40.

See:

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like