DEV Community

Cover image for Scanners
Tristan Elliott
Tristan Elliott

Posted on

Scanners

Introduction

  • The third post in the streams series, it will be on scanning in Java. I have created a YouTube video version, so please make sure to check that out as well.

Scanning and Formatting

  • Programming I/O often includes translating data to and from neatly formatted data that we humans like to read. Doing this by hand can be rather tedious, so Java has given us two APIs in order to help with this. The Scanner API and the Formatting API. In this blog post I will be walking us through the scanner API.

Scanner

  • What is a scanner? Well a scanner is a class in Java that is used for obtaining the input of Java primitive types. The scanner then takes the data that it collects and breaks them into "tokens". These tokens are defined by any white space between characters. We should use a scanner in java when we have a data source that is external to our program and we want to parse data from it. Parsing simply means that we want to change the data somehow. Now that we have a basic understanding of a scanner, lets look at some code
import java.io.*;
import java.util.Scanner;

public class ScanXan {
    public static void main(String[] args) throws IOException {

        Scanner s = null;

        try {
            s = new Scanner(new BufferedReader(new FileReader("xanadu.txt")));

            while (s.hasNext()) {
                System.out.println(s.next());
            }
        } finally {
            if (s != null) {
                s.close();
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode
  • 1) main : this signifies the main method in Java and it is very important to any Java Application. It acts as the starting point for the JVM(Java Virtual Machine).

  • 2) throws : this is a part of the java exception handling, it signals to the compiler that there will be exception handling for this method somewhere else. Essentially throws tells the JVM to look somewhere else for the appropriate exception handler. However, in this code we do not handle the exception and any exceptions break the code.

  • 3) IOException : this signals that some kind of I/O exception might happen inside this method. This class is a general class of exceptions produced by failed I/O operations.

  • 4) try{} : this is called a try block and any code that might produce an exception should go inside of here.

  • 5) new BufferedReader() : the bufferedReader class makes character input streams more efficient. It is used to wrap around any Reader whose read() method could be costly. In our case the Reader is the FileReader class. So instead of reading directly from the stream, our scanner now reads from the buffer returned by BufferedReader(). This means fewer I/O operations, which makes our program more efficient.

  • 6) new FileReader("xanadu.txt") : this is a convivence class for reading character files. It will return a a character stream from the file "xanadu.txt" located in the root directory.

  • 7) new Scanner(new BufferedReader(new FileReader("xanadu.txt")) : so putting it all together, first a character stream is created by FileReader and it consists of the contents from "xanadu.txt". Then we wrap it with a BufferedReader(), which eliminates unnecessary and unwanted I/O operations making our code more efficient. Finally we use the newly created buffered stream to create our scanner and the scanner creates its tokens, which we will later use to parse our data.

  • 8) s.hasNext() : this is used in the while loop and it is what allows our while loop to keep looping. hasNext() is a method on our scanner object and it will return true as long as there is a token inside of the scanner. On each interaction of the while loop we are printing out the next token of the scanner with System.out.println(s.next()).

  • 9) finally{} : we then enter the finally block. This block of code will run no matter what and inside of it we will close the scanner. It will also close and flush the underlying stream.

Conclusion

  • Thank you for taking the time out of you day to read this blog post of mine. If you have any questions or concerns please comment below or reach out to me on Twitter.
  • Also make sure to checkout my YouTube channel for more programming tutorials.

Top comments (0)