Quantcast
Channel: Baeldung
Viewing all articles
Browse latest Browse all 3526

Java Scanner

$
0
0

1. Overview

In this quick tutorial, we'll illustrate how to use the Java Scanner class – to read input, 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 contains “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're closing 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();
}

Similar to 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 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 simply 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 if we read through Scanner we'll be reading 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 after the integer, there is a line separator, 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 will place Scanner to the starting of 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 “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 comma “,” and dash”” as delimiters to scan a file contains “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. Conclusion

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

We learned how to read input from file, Console or String using Scanner; we also learned how to find and skip a pattern using Scanner – as well as how to change the Scanner delimiter.

The implementation of these examples can be found over on GitHub.


Viewing all articles
Browse latest Browse all 3526

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>