Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Introduction

The Scanner class is a useful tool in Java for reading inputs from the console. We often use the next() or nextLine() methods to read each input on a separate line. However, sometimes we may want to read multiple inputs on the same line.

In this tutorial, we’ll explore different ways to achieve this, such as using a space or a custom delimiter, or even regular expressions.

2. Reading Multiple Inputs on the Same Line

To read multiple inputs on the same line, we can use the Scanner class along with the next() or nextLine() method. However, it would help if we used a delimiter to separate each input.

2.1. Using Space as Delimiter

One way to read multiple inputs on the same line is to use a space as a delimiter. Here’s an example:

Scanner scanner = new Scanner(System.in);
System.out.print("Enter two numbers: ");
int num1 = scanner.nextInt();
int num2 = scanner.nextInt();
System.out.println("You entered " + num1 + " and " + num2);

In this example, we use the nextInt() method to read two integers from the console. Since we’re reading them on the same line, we use a space as a delimiter to separate the two integers.

2.2. Using Custom Delimiter

If we don’t want to use a space as a delimiter, we can use a custom delimiter by calling the use the setDelimiter() method on the Scanner object. Here’s an example:

Scanner scanner = new Scanner(System.in);
scanner.useDelimiter(";");
System.out.print("Enter two numbers separated by a semicolon: ");
int num1 = scanner.nextInt();
int num2 = scanner.nextInt();
System.out.println("You entered " + num1 + " and " + num2);

In this example, we use a semicolon as a delimiter instead of a space. We also call the setDelimiter() method to set the delimiter to a semicolon.

2.3. Using Regular Expressions as Delimiters

In addition to using a space or a custom delimiter, we can also use regular expressions as delimiters when reading multiple inputs on the same line. Regular expressions are patterns that can match strings flexibly and powerfully.

For example, if we want to read multiple inputs on the same line separated by either a space or a comma, we can use the following code:

Scanner scanner = new Scanner(System.in);
scanner.useDelimiter("[\\s,]+");
System.out.print("Enter two numbers separated by a space or a comma: ");
int num1 = scanner.nextInt();
int num2 = scanner.nextInt();
System.out.println("You entered " + num1 + " and " + num2);

We use the regular expression [\\s,]+ as the delimiter in this example. This regular expression matches one or more spaces or commas.

3. Error Handling

When reading multiple inputs on the same line, it’s important to handle errors that may occur. For example, the program will throw an exception if the user enters an invalid input, such as a String instead of an Integer.

To handle this error, we can use a try-catch block to catch and handle the exception gracefully. Here’s an example:

Scanner scanner = new Scanner(System.in);
scanner.useDelimiter(";");
System.out.print("Enter two numbers separated by a semicolon: ");
try {
    int num1 = scanner.nextInt();
    int num2 = scanner.nextInt();
    System.out.println("You entered " + num1 + " and " + num2);
} catch (InputMismatchException e) {
    System.out.println("Invalid input. Please enter two integers separated by a semicolon.");
}

In this example, we use a try-catch block to catch the InputMismatchException that may be thrown if the user enters an invalid input. If this exception is caught, we print an error message and ask the user to enter the inputs again.

4. Conclusion

In this article, we discuss how to read multiple inputs on the same line using the Scanner class.

The full example code is available over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are closed on this article!