Course – LS – All

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

>> CHECK OUT THE COURSE

1. Overview of Scanner

In this quick tutorial, we’ll illustrate how to use the Java Scanner class – to read input and find and skip patterns with different delimiters.

2. Scan a File

First – let’s see how to read a file using Scanner.

In the following example – we read a file containing “Hello world” into tokens:

@Test
public void whenReadFileWithScanner_thenCorrect() throws IOException{
    Scanner scanner = new Scanner(new File("test.txt"));

    assertTrue(scanner.hasNext());
    assertEquals("Hello", scanner.next());
    assertEquals("world", scanner.next());

    scanner.close();
}

Note that the next() method returns the next String token here.

Also, note how we close the scanner when we’re done using it.

3. Convert InputStream to String

Next – let’s see how to convert an InputStream into a String using a Scanner:

@Test
public void whenConvertInputStreamToString_thenConverted()
  throws IOException {
    String expectedValue = "Hello world";
    FileInputStream inputStream 
      = new FileInputStream("test.txt");
    
    Scanner scanner = new Scanner(inputStream);
    scanner.useDelimiter("A");

    String result = scanner.next();
    assertEquals(expectedValue, result);

    scanner.close();
}

Like the previous example, we used the Scanner to tokenize the entire stream from the beginning to the next regex “A” – which matches the full input.

4. Scanner vs. BufferedReader

Now – let’s discuss the difference between Scanner and BufferedReader – we generally use:

  • BufferedReader when we want to read the input into lines
  • Scanner to read the input into tokens

In the following example – we’re reading a file into lines using BufferedReader:

@Test
public void whenReadUsingBufferedReader_thenCorrect() 
  throws IOException {
    String firstLine = "Hello world";
    String secondLine = "Hi, John";
    BufferedReader reader 
      = new BufferedReader(new FileReader("test.txt"));

    String result = reader.readLine();
    assertEquals(firstLine, result);

    result = reader.readLine();
    assertEquals(secondLine, result);

    reader.close();
}

Now, let’s use Scanner to read the same file into tokens:

@Test
public void whenReadUsingScanner_thenCorrect() 
  throws IOException {
    String firstLine = "Hello world";
    FileInputStream inputStream 
      = new FileInputStream("test.txt");
    Scanner scanner = new Scanner(inputStream);

    String result = scanner.nextLine();
    assertEquals(firstLine, result);

    scanner.useDelimiter(", ");
    assertEquals("Hi", scanner.next());
    assertEquals("John", scanner.next());

    scanner.close();
}

Note how we’re using the Scanner nextLine() API – to read the entire line.

5. Scan Input From the Console Using New Scanner(System.in)

Next – let’s see how to read input from the Console using a Scanner instance:

@Test
public void whenReadingInputFromConsole_thenCorrect() {
    String input = "Hello";
    InputStream stdin = System.in;
    System.setIn(new ByteArrayInputStream(input.getBytes()));

    Scanner scanner = new Scanner(System.in);

    String result = scanner.next();
    assertEquals(input, result);

    System.setIn(stdin);
    scanner.close();
}

Note that we used System.setIn(…) to simulate some input coming from the Console.

5.1. nextLine() API

This method returns the string at the current line:

scanner.nextLine();

This reads the content of the current line and returns it except for any line separator at the end – in this case – the new line character.

After reading the content, Scanner sets its position to the start of the next line. The important point to remember is that the nextLine() API consumes the line separator and moves the position of the Scanner to the next line.

So the next time we read through Scanner, we’ll read from the start of the next line.

5.2. nextInt() API

This method scans the next token of the input as an int:

scanner.nextInt();

The API reads the integer token available next.

In this case, if the next token is an integer and there is a line separator after the integer, always remember that nextInt() will not consume the line separator. Instead, the position of the scanner will be the line separator itself.

So if we have a series of operations, where the first operation is a scanner.nextInt() and then scanner.nextLine() and as an input, if we provide an integer and press line break, both the operations will be executed.

The nextInt() API will consume the integer, and the nextLine() API will consume the line separator and place Scanner at the start of the next line.

6. Validate Input

Now – let’s see how to validate input using a Scanner. In the following example – we use the Scanner method hasNextInt() to check if the input is an integer value:

@Test
public void whenValidateInputUsingScanner_thenValidated() 
  throws IOException {
    String input = "2000";
    InputStream stdin = System.in;
    System.setIn(new ByteArrayInputStream(input.getBytes()));

    Scanner scanner = new Scanner(System.in);

    boolean isIntInput = scanner.hasNextInt();
    assertTrue(isIntInput);

    System.setIn(stdin);
    scanner.close();
}

7. Scan a String

Next – let’s see how to scan a String using Scanner:

@Test
public void whenScanString_thenCorrect() 
  throws IOException {
    String input = "Hello 1 F 3.5";
    Scanner scanner = new Scanner(input);

    assertEquals("Hello", scanner.next());
    assertEquals(1, scanner.nextInt());
    assertEquals(15, scanner.nextInt(16));
    assertEquals(3.5, scanner.nextDouble(), 0.00000001);

    scanner.close();
}

Note: The method nextInt(16) reads the next token as a hexadecimal integer value.

8. Find Pattern

Now – let’s see how to find a Pattern using Scanner.

In the following example – we use findInLine() to search for a token that matches the given Pattern in the entire input:

@Test
public void whenFindPatternUsingScanner_thenFound() throws IOException {
    String expectedValue = "world";
    FileInputStream inputStream = new FileInputStream("test.txt");
    Scanner scanner = new Scanner(inputStream);

    String result = scanner.findInLine("wo..d");
    assertEquals(expectedValue, result);

    scanner.close();
}

We can also search for a Pattern in the specific domain using findWithinHorizon(), as in the following example:

@Test
public void whenFindPatternInHorizon_thenFound() 
  throws IOException {
    String expectedValue = "world";
    FileInputStream inputStream = new FileInputStream("test.txt");
    Scanner scanner = new Scanner(inputStream);

    String result = scanner.findWithinHorizon("wo..d", 5);
    assertNull(result);

    result = scanner.findWithinHorizon("wo..d", 100);
    assertEquals(expectedValue, result);

    scanner.close();
}

Note that the search horizon is simply the number of characters within which the search is performed.

9. Skip Pattern

Next – let’s see how to skip a Pattern in Scanner. We can skip tokens that match a specific pattern while reading the input using Scanner.

In the following example – we skip the “Hello” token using the Scanner method skip():

@Test
public void whenSkipPatternUsingScanner_thenSkipped() 
  throws IOException {
    FileInputStream inputStream = new FileInputStream("test.txt");
    Scanner scanner = new Scanner(inputStream);

    scanner.skip(".e.lo");

    assertEquals("world", scanner.next());

    scanner.close();
}

10. Change Scanner Delimiter

Finally – let’s see how to change the Scanner delimiter. In the following example – we change the default Scanner delimiter to “o“:

@Test
public void whenChangeScannerDelimiter_thenChanged() 
  throws IOException {
    String expectedValue = "Hello world";
    String[] splited = expectedValue.split("o");

    FileInputStream inputStream = new FileInputStream("test.txt");
    Scanner scanner = new Scanner(inputStream);
    scanner.useDelimiter("o");

    assertEquals(splited[0], scanner.next());
    assertEquals(splited[1], scanner.next());
    assertEquals(splited[2], scanner.next());

    scanner.close();
}

We can also use multiple delimiters. In the following example – we use both commas “,” and dash”” as delimiters to scan a file containing “John,Adam-Tom“:

@Test
public void whenReadWithScannerTwoDelimiters_thenCorrect() 
  throws IOException {
    Scanner scanner = new Scanner(new File("test.txt"));
    scanner.useDelimiter(",|-");

    assertEquals("John", scanner.next());
    assertEquals("Adam", scanner.next());
    assertEquals("Tom", scanner.next());

    scanner.close();
}

Note: The default Scanner delimiter is whitespace.

11. Handling NoSuchElementException

Before diving deep into the details, let’s understand what the exception means.

According to the documentation, NoSuchElementException typically occurs when the requested element does not exist.

Basically, Scanner throws this exception when it fails to read data or fetch the next element.

11.1. Reproducing the Exception

Now that we know what causes Scanner to throw NoSuchElementException. Let’s see how to reproduce it using a practical example.

To demonstrate a real-world use case, we’re going to read data from a FileInputStream twice using two different instances of the Scanner class:

public void givenClosingScanner_whenReading_thenThrowException() throws IOException {
    final FileInputStream inputStream = new FileInputStream("src/test/resources/test_read.in");

    final Scanner scanner = new Scanner(inputStream);
    scanner.next();
    scanner.close();

    final Scanner scanner2 = new Scanner(inputStream);
    scanner2.next();
    scanner2.close();
}

Next, let’s run our test case and see what happens. Indeed, looking at the logs, the test case fails with NoSuchElementException:

java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:862)
    at java.util.Scanner.next(Scanner.java:1371)
    at com.baeldung.scanner.JavaScannerUnitTest.givenClosingScanner_whenReading_thenThrowException(JavaScannerUnitTest.java:195)
...

11.2. Investigating the Cause

Simply put, when a Scanner is closed, it will close its source, too, if the source implements the Closeable interface.

Basically, when we invoked scanner.close() in our test case, we actually closed the inputStream, too. So, the second Scanner failed to read the data because the source was not open.

11.3. Fixing the Exception

There’s only one way to fix the exception – to avoid using multiple Scanners to manipulate the same data source.

So, a good solution would be using a single Scanner instance to read data and close it at the very end.

12. nextLine() After nextXXX() Methods

Typically, the nextXXX() methods don’t consume the newline character in the input created by hitting the “Enter” key.

For instance, calling nextInt() will only read the next integer value and doesn’t read the newline character.

So, when we subsequently call nextLine() to read the next input, it reads the newline character left over from the previous call:

Scanner scanner = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = scanner.nextInt();
System.out.print("Enter your first name: ");
String firstName = scanner.nextLine(); // Skipped because it reads the remaining newline character

To avoid this behaviour, we need to add another nextLine() just after the nextInt() method to consume the remaining newline character:

System.out.print("Enter your age: ");
int age = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter your first name: ");
String firstName = scanner.nextLine();

Here, scanner.nextInt() will read the int input. Then, the first scanner.nextLine(), will consume the newline character created by hitting the “Enter” key.

Finally, the last scanner.nextLine() will do its job and read the user’s first name input.

Alternatively, we can use nextLine() to capture the age value. However, in this case, we need to convert the given input to int using Integer.parseInt():

int age = Integer.parseInt(scanner.nextLine());
String firstName = scanner.nextLine();

The second nextLine() method will read the next input.

Another solution would be using the skip() method to ignore the leftover return line:

int age = scanner.nextInt();
scanner.skip("\r\n");
String firstName = scanner.nextLine();

That way, we tell Scanner to skip the return line and let the nextLine() method to read the next user input.

13. Conclusion

In this tutorial, we went over multiple real-world examples of using the Java Scanner.

We learned how to read input from a file, console, or String using Scanner. We also learned how to find and skip a pattern using Scanner and how to change the Scanner delimiter.

Finally, we explained how to handle the NoSuchElementException exception.

The implementation of these examples can be found 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)
2 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Comments are closed on this article!