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

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

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest