Sunday, March 13, 2016

Intro To Java: Section 2.4 - Importing Scanners

In this section we will discuss using Scanners to take user input from the console.

Watch Lecture Video 2.4


One of the most useful things about computers is their ability to take user input and perform computations. In the example programs we've looked at so far all of the variable values have been hard coded into the programs - but this severely limits their functionality. A program that only calculates the area of a circle with a radius of 1.5 is pretty useless to most people. What we need is a program that asks the user the radius, and can then find the area of any circle. We do this with user input. In Java, you can read user input from the console using the scanner object.

The scanner object can be found within the "util" package of the Java API. As was stated in the previous lesson the Java API includes all the objects you can use by default with Java. This is what fundamentally makes Java an object oriented programming language. We can access objects from the Java API by importing them into our java files using an import statement. The diagram to the right visually represents how this happens.

Using the Import Statement

You can use the keyword import followed by the name of the package you want to import from the Java API. In this case, the scanner class is stored with "util" package, so our import statement looks as follows:

import java.util.Scanner;

All import statements can be placed directly at the top of the class file, right after the package declarations. If you create a new Java project with an import statement it should look something like what follows:

package test1;

//import at beginning of the file after package
import java.util.Scanner;

public class main {

 public static void main(String[] args) {
  //Todo: Create a scanner and use it


Creating & Using The Scanner

Now that we have imported the scanner class we can begin using it. To create any object in follow you will use the following syntax:

ObjectType objectName = new ObjectType(Arguments);

In the case of scanners, the object type is "Scanner," you may name the scanner whatever you want. The argument we use to create a scanner is a input stream object - this essentially lets the scanner that it should be expecting input. Write the following code in the main method of your test program.

Scanner userInput = new Scanner(;

Once the scanner has been created, you can use it to get user input and store it in a variable. It is important that you use the right scanner method to get the proper data type (i.e. don't look for integers when users are entering strings). The example below illustrates how this is done and how the scanner is used. You may also view example console output below.

//import at beginning of the file after package
import java.util.Scanner;

public class main {

 public static void main(String[] args) {
  //Create the scanner
  Scanner userInput = new Scanner(;
  //Get user input
  System.out.println("Please enter a number.");
  //Create variable "userNumberEntered" and set it to the next integer the user enters
  int userNumberEntered = userInput.nextInt();
  //Print out the value of the number
  System.out.println("You have entered the number: " + userNumberEntered);
  //Close the scanner

Please enter a number.
You have entered the number: 5

Note that in addition to using the .nextInt() method, you may also use the .nextLine() and .nextDouble() methods to get the values of strings and doubles respectively. Also pay attention to the last line of code in the program which closes the scanner after we are done using it. This will be important in the future to prevent resource leaks. Make sure you always close the scanner when you're done using it.

Review Exercise 2.4: Calculator

Instructions: Given the knowledge you've gained in this section on how to use and manipulate variables, create a program that allows the user to enter two double values. The program should then find the sum, product, quotient, and remainder of the two values and print them out.

Stop: Before you begin reading the suggested methodology below, try to think about the steps you need to take to make this application yourself. Try to create the program yourself without following my instructions word for word, then come back and see how I did it if you still need help.

Suggested Methodology:

  1. Create a new project called "BasicCalculator"
  2. Create a new class ""
  3. Import the Scanner and setup the main method
  4. Create the input Scanner
  5. Ask the user to input values and save their input as doubles using the .nextDouble() method on the scanner
  6. Create separate variables and use them to compute the sum, product, quotient, and remainder.
  7. Print the values of the variables to the screen
  8. Close the scanner
  9. Run your program to see if it's working and compare it to the source code I've provided

Sample Output

Welcome to Calculator.
Please enter first value: 
Please enter second value:
The results are as follows:
Sum 100.000000 + 10.000000 = 110.000000 
Product 100.000000 x 10.000000 = 1000.000000 
Quotient 100.000000 / 10.000000 = 10.000000 
Remainder 100.000000 / 10.000000 = 0.000000 

Debugging Tips:

Review your code for syntax errors, improperly spelled object/variable names, missing semicolons, brackets, etc.

If you run into any issues or errors when you attempt to execute your program try the following:

  • Pay attention to where the error occurred and attempt to read the error message to determine what you did wrong
  • Make sure you created and use all of your objects properly, that they all have proper arguments.
  • Make sure you're using the correct data type for the operation you're doing (i.e. don't add a string to a double)
  • Double and triple check code for syntax errors
  • Compare your code to my example code
  • Post your code in the forums and ask for help
Watch Exercise Walkthrough Video View Calculator Solution


Post a Comment