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

Java Scanner

$
0
0

1. Overview

In this tutorial, we’ll illustrate how to use the Java Scanner 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 contains “Hello world” into tokens using Scanner:

@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: The Scanner method next() returns the next String token.

3. Convert InputStream to String

Next – let’s see how to convert an InputStream into a String using 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();
}

Note: In the previous example we tricked the Scanner to tokenize the entire stream from the beginning to the next regex “\A” (which matches the beginning of 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: We can also use the Scanner nextLine() API to read the entire line.

5. Scan Input from Console

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

@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.

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 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 example 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 in the github project – this is an Eclipse based project, so it should be easy to import and run as it is.

Java 8 is out and about now - to keep track of everything, I will keep updating the Gran Kahuna of Java 8 Resources.

I usually post about Java stuff on Google+ - you should follow me there:


Viewing all articles
Browse latest Browse all 3522

Trending Articles



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