Scanner and I/O


Premium Content - Free Preview

User Interface
In BlueJ, you are able to interact directly with Classes and invoke their methods. However, a program in the real world runs without BlueJ, so it needs some interface for communication with the user. While many programs use a visual interface, it's simpler to use an all-text interface. We saw this previously when printing output, and we'll now see how to get the user's text input into the program with the Scanner class. This node will quickly cover the basics of Scanner, and you can refer to the official docs and the Learneroo Scanner reference for more information.

STDIN
The Scanner is used in Java to parse text input into useful pieces. Input can come from different sources, such as a text file. In this node, we will be getting input from the STDIN, or Standard Input. This is direct text input that comes into the program through the terminal. The terminal is where output is displayed when you type System.out.print and it can also be used to get input with System.in.

Task: In the BlueJ code pad, type System.out.println("hello");
This will cause the terminal to pop up and display the output "hello". The terminal can also be used for typing in input, as will be shown below.

blueJ-ternimal

Question: What is System?
Answer: It's a special Java class in the built-in java.lang package that has a few useful methods. You cannot create instances of System, but you can use its special fields System.in and System.out to access the Standard Input and Output.

Creating a Scanner
To use the Scanner, you need to import it, as seen in the last node. The Scanner has many different constructors, depending on what you want to scan in. To get input from the STDIN, you can create the following Scanner:

Scanner in = new Scanner(System.in);

next()
Now that you created the Scanner (and named it in), you can use its methods to read from the Standard Input. Scanner includes a method called next() for getting the next token of input. Tokens, by default, are space-separated text, which can also be called "words" in English. For example, if the program contains the following code:

String word1 = in.next();

...and the user types in

hello world

The Scanner will grab the first word typed ("hello") and assign it to word1.

Scanner Methods
If you look at the methods of Scanner, you'll notice that many of them have similar names, like hasNextThing() and nextThing(). For example, there's hasNextInt() and nextInt(). The "hasNextThing" methods are used to check if the next token of text input is of type "Thing". For example, hasNextInt() tells you if the next token of input can be interpreted as an integer. The "nextThing" methods take in the next token and converts it to type "Thing". For example, nextInt() reads the next token and converts it to an int to return. If the next token cannot be converted to an int, nextInt() will throw an error.

input code returns
42 hasNextInt() true
hello hasNextInt() false
42 nextInt() 42 (as an int)
hello nextInt() Error! (InputMismatchException)

The hasNextThing methods are usually used together with the nextThing methods to make sure the next Token is the right type. For example, given a Scanner in, you can use an if statement to get number input:

if( in.hasNextInt() ){
  int num = in.nextInt();
  //...
}

Or a while statement, such as this one which scans in words of input:


End of Free Content Preview. Please Sign in or Sign up to buy premium content.

Comments

  • BlueJ do not want to compile "switch(input)" because input is String. What should I do ?

  • If you're using Java 6, you should use an if-else statement instead.

  • Is it normal to initialize your Notebook in a menu class? Should I put it somewhere else? Would it be smart to create an Initialize class?

  • In this case I think it's fine. In a larger program it's true that it might make sense to create the Notebook separately.

Contact Us
Sign in or email us at [email protected]