My Other Sites

Friends

Java Scanner Weirdness

The most common problems I see in Java amongst newbies to the language is the use of the Scanner class. This particular class was created in order to make reading in data input from the command line easier for those new to the language. However, I fear it only half worked. Scanner is still a big pain to use.

First of all, do not even ATTEMPT to mix data types with the same scanner. It will not work. Having a Scanner read in a line of text and then read a single integer will cause an exception to be thrown, and then the person first learning Java gets a big headache and hasn’t a clue about what is wrong with the code. The fix for this by the way is to simply have different Scanners work over System.in, making sure each one reads the data in only one format.

The next biggest problem with Scanner that I see is the use of the hasNext() method. You cannot use the hasNext() method effectively when reading keyboard input from the command line. Usually I see this kind of code:

while (input.hasNext()) {

System.out.println(input.next());

}

If you cannot see the problem with this while loop, it’s because you have not realized that in this case, input is a scanner looking at System.in . The only way this loop will stop is if the end of the file is reached, and well, the command line can never reach the end of the file. This means the only way to stop this loop would be to input the EOF representation into the keyboard, which most people do not know.

Scanner has its problems, and if you want to advance as a programmer, I highly suggest using a BufferedReader. It’s a little more complicated but much less of a hassle later on, especially when you begin getting into much more complicated streams of data.

Tags: , , ,

4 Responses to “Java Scanner Weirdness”

  1. KuchtaFish says:

    Alberto, I think you’re wrong. I run this:

    import java.util.Scanner;
    public class albertoswrong {
    public static void main(String[] args) {
    Scanner inScan = new Scanner(System.in);
    System.out.println(“line1=” + inScan.nextLine());
    System.out.println(“int2=” + inScan.nextInt());
    System.out.println(“string3=” + inScan.next());
    }
    }

    And get the following (> demarks input):
    >This is the first line
    line1=This is the first line
    >3
    int2=3
    >somestring someotherstring
    string3=somestring

    Also, I think you’re taking a simplistic view of Scanner. While it is often used by CS123 students to handle user input, it has some handy features for more advanced stuff. It’s really useful when someone gives you a pile of data in a consistent but non-standard format; Scanner + regex = easy parsing of complex data formats. It really saved me a lot of trouble when I needed to parse through a huge pile of election results and census data to input to a neural network I was writing for Intro to AI.

  2. b says:

    Your site (schedule) is broken in IE

  3. Alberto says:

    Yea I know : /

  4. Alberto says:

    Hmm interesting. I know that Scanner has messed me up on a number of occasions when using it to output different types of data. I had a for loop that cycled through string, int, and double data and it didn’t like that at all.

    I honestly have never used it with regex, so I’d definitely have to try it some time.

Leave a Reply