Skip to main content

Section 4.8 Worked Example: Call a method of Scanner

Subgoals for Calling a Method.

  1. Classify method as static method or instance method
    1. If static, use the class name
    2. If instance, must have or create an instance
  2. Write (instance / class) dot method name and ( )
  3. Determine whether parameter(s) are appropriate
    1. Number of parameters passed must match method declaration
    2. Data types of parameters passed must be compatible with method declaration
  4. Determine what the method will return and where it will be stored
  5. Evaluate right hand side of assignment. Value is dependent on method’s purpose

Subsection 4.8.1

You can watch this video or read through the content below it.

Subsection 4.8.2 Problem Statement

Given the following declaration and instantiation of a Scanner object to read input from the user, write the code to read in a double and store it in the variable value.
Scanner input = new Scanner(System.in);

Subsection 4.8.3 SG1: Classify method as static method or instance method

First we need to determine what method we need to call. In looking at the documentation for the Scanner class, we see the nextDouble() method which will read in a double value. Note that there is no keyword static in the documentation for this method, so we can assume that it is an instance method. Because we want to call an instance method, we need to identify the instance which will call the method. In this example we have already created a Scanner instance which is named input.
Figure 4.8.1.

Subsection 4.8.4 SG2: Write (instance / class) dot method name and ( )

Here is the method call with the instance:
input.nextDouble()

Subsection 4.8.5 SG3: Determine whether parameter(s) are appropriate

No parameters required (look at the API again to verify this). When you are just learning to program, you won’t know any of these APIs and may have to look things up frequently. As you progress you will find that you will automatically begin to memorize the most frequently used methods, however, it is never inappropriate to look things up to be verified.

Subsection 4.8.6 SG4: Determine what the method will return and where it will be stored

According to the API documentation, the nextDouble method returns a double. Our code can choose to store the returned double value or ignore it, in which case the value will be lost. For this example we are asked to store the value read in from the user to a variable named value. Because we do not yet have a double variable declared, we need to declare that variable. The final code looks like:
double value = input.nextDouble();

Subsection 4.8.7 SG5: Evaluate right hand side (RHS) of assignment. Value is dependent on method’s purpose

The nextDouble method reads the next value from the input stream and attempts to convert it into a decimal value and returns a double. So the RHS is a double and the LHS is a double variable, so the assignment statement is valid.
double value = input.nextDouble();

Practice Pages.

You have attempted of activities on this page.