Program Logic

Unit 4 Index

Scanner Library Methods

Scanner Library Methods

The Scanner has many built-in library methods that will help you gather and validate end-user input. You do not need to know the underlying code of these methods, but you do need to now how to use them and choose the most appropriate method for the situation. This chapter covers Scanner methods you are likely to use in Program Logic. Some of the method names are pretty intuitive, which helps you to know what they do.

The Scanner Class

We are already familiar with Scanner Class and some of its library methods. Just remember these important things:

Scanner Methods that Return a String

Both the .next() and .nextLine() Scanner methods return whatever the end-user keyed into the console window. The .next() method reads input until it encounters a space. The .nextLine() method reads input unil it detects the enter key was pressed.

Use the .nextLine() method when you want to get a multi-word input from the end-user. Use the .next() method when you want to get a single word from the end-user. The .next() method can also be used to get a single number from the end-user. Grabbing a number using the .next() method will not return an input mismatch exception, so using .next() is one strategy to keep your program from abnormally ending. However, if you are going to do some math on the number, you must validate that the value is a number and the convert it to an actual number data type before doing the math.

Scanner Methods that Return a Number

Both the .nextInt() and .nextDouble() Scanner methods return end-user input as long as the input the appropriate number for the method. The .nextInt() will only work properly when the end-user enters a whole number. The .nextDouble()method works if the end-user enters either a whole number or a number that contains a decimal point. Both methods read input unil they detect the enter key was pressed.

Both methods throw an input mismatch exception if the correct number type is not entered by the end-user. This error, causes your application to fail. Use .nextDouble() when you want one whole number or decimal number from the end-user. Use the .nextInt() method when you want one whole number from the end-user.

Scanner Methods that Return a Boolean

Never trust the end-user's input. Always validate their input before trying to use it. Fortunately, the Scanner has a few methods that can help with end-user validate. You will notice by the method names, that these methods return a Boolean.Therefore, in order to use this type of Scanner method, you need to put it in a decision structure. Do not worry, we have a entire unit on decision structures, but here is enough to get you started.

//simple if statement
if(whatever is between the parentheses is true){
  //then any code between the curly braces will run
}

The .hasNextDouble() Method

The method .hasNextDouble() is one way to validate end-user input before trying to use it. The .hasNextDouble() method causes the Scanner to pre-read the end-user input. Essentially, you are asking the Scanner if it detected a double value. The Scanner returns true if it detected a double or int value. Only if true, is returned, can the code between the curly braces run. Please study the code below.

//decision structure with .hasNextDouble()
if(input.hasNextDouble()){
  myDoubleNum = input.nextDouble();
}

The .hasNextInt() Method

The method .hasNextInt() is another way to validate end-user input before trying to use it. The .hasNextInt() causes the Scanner to pre-read the end-user input. Essentially, you are asking the Scanner if it detected an int value. The Scanner returns true if it detect an int value. Only if true, is returned, can the code between the curly braces run. Please study the code below.

//decision structure with .hasNextInt()
if(input.hasNextInt(){
  myIntNum = input.nextInt();
}

End-user Input Validation

As I am sure you figured out, the Scanner is used for getting data from the end-user and print statements are used to give information to the end-user. In this course, the end-user enters information into the console and you grab it using one or more of the Scanner methods listed above.

Below is pseudocode for gathering and validating end-user input. Pseudocode is just putting your coding thoughts into words, so you can get your head around what needs to happen without worrying about specific syntax. Remember, the end-user must be told what to enter.

1. Declare double price that will hold the end-user's input
2. Instantiate the Scanner object using input as its variable

3. Print to the console "How much did your item cost?"

4. If the end-user entered the appropriate number value
5. Then save the entry into the price variable
6. Print a statement that uses price.

Let's translate the pseudocode into Java. I numbered the pseudocode steps so they can be easily identified within the Java code. In Java, two slashes mean a comment. So each line of Java code is commented with the corresponding pseudocode number. Remember, string literals are always in quotes whereas variables are not. Please study the below code.

double price = 0.0; //1
Scanner input = new Scanner(System.in); //2

System.out.println("How much did your item cost?"); //3
if(input.hasNextDouble()){ //4
  price = input.nextDouble(); //5
  System.out.println("Your item cost: $" + price); //6
} 

Critical Thinking

What do you think happens if the end-user enters text instead of a double or int? Think about it and check your assumptions by writing and running the code in a Java project.

What You Learned

Assignment Information

What's next?

This is the last chapter in this unit. Please complete the assignment preparation tasks and assignment tasks outlined in Blackboard.