Back

Activity 1.7 Numeric Input

divider

Introduction

Activity 1.7

Numeric Input

Topics

  • Numeric input methods with Scanner
  • Issues when getting string and numeric input with Scanner

Scanner.nextInt() and Scanner.nextDouble()

When numeric input is necessary, use Scanner.nextInt() and Scanner.nextDouble().

Scanner input = new Scanner(System.in);
System.out.print("Enter your ID: ");
int id = input.nextInt();
System.out.print("Enter your balance: $");
double balance = input.nextDouble();
Terminal window
Enter your ID: 20255555 [Enter]
Enter your balance: $55.59 [Enter]

Scanner.nextInt() vs. Scanner.nextDouble()

Use Scanner.nextInt() when you need a whole number (student ID, age, number of people, etc.).

Use Scanner.nextDouble() when you need a decimal number (dollar amount, weight, GPA, etc.).

Issues when getting string and numeric input

When you press the Enter key after typing a number, it leaves a hidden new-line character\n in the input stream. Methods like Scanner.nextInt() and Scanner.nextDouble() read only the number, but they leave the new-line character behind.

If your program then asks for a string using Scanner.nextLine(), it will immediately read the leftover new-line character as an empty string and skip the input you were expecting to type.

Issues when getting string and numeric input

Scanner input = new Scanner(System.in);
System.out.print("Enter the distance: ");
double id = input.nextDouble();
System.out.print("Enter the unit: ");
String unit = input.nextLine();
Terminal window
Enter the distance: 10 [Enter]
Enter the unit: [Enter]

Issues when getting string and numeric input

Fix: Use an extra Scanner.nextLine() to "consume" the leftover \n
Scanner input = new Scanner(System.in);
System.out.print("Enter the distance: ");
double id = input.nextDouble();
input.nextLine(); // Consume the leftover \n
System.out.print("Enter the unit: ");
String unit = input.nextLine();
Terminal window
Enter the distance: 10 [Enter]
Enter the unit: Km [Enter]

Key Terms

Input Stream
The flow of data from a source, such as a keyboard, into a program. When you type and press enter, the characters you've typed are sent to this stream. Methods from Scanner read from this stream.

'F' → Fullscreen

Objectives

  • icon Prompting a user for numeric input
  • icon Performing math operations with user input
divider

Activity Tasks

  • icon Create a new project named 1-7-Numeric-Input.
  • icon Complete each task individually.

Task 1: Initialize Scanner

Program.java
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
}
}

Task 2: Age to Months Calculator

Program.java
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("--- Demo 1 - Age to Months Calculator ---");
System.out.print("Enter your age: ");
int age = input.nextInt();
System.out.println("You are " + age + " years old. That's " + (age * 12) + " months old!");
input.nextLine(); // Consume leftover \n [Enter] from numeric input
System.out.print("Press Enter to continue...");
input.nextLine();
//
}
}

Task 3: Square Area Calculator

Program.java
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("--- Demo 1 - Age to Months Calculator ---");
System.out.print("Enter your age: ");
int age = input.nextInt();
System.out.println("You are " + age + " years old. That's " + (age * 12) + " months old!");
input.nextLine(); // Consume leftover \n [Enter] from numeric input
System.out.print("Press Enter to continue...");
input.nextLine();
// -----------------------------------------------------------------------------
System.out.println("\n--- Demo 2 - Square Area Calculator ---");
System.out.print("Enter the length of the square: ");
double length = input.nextDouble();
double area = length * length;
System.out.println("The area of the square is " + area);
input.nextLine(); // Consume leftover \n [Enter] from numeric input
System.out.print("Press Enter to continue...");
input.nextLine();
//
}
}

Task 4: Score Calculator

Program.java
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("--- Demo 1 - Age to Months Calculator ---");
System.out.print("Enter your age: ");
int age = input.nextInt();
System.out.println("You are " + age + " years old. That's " + (age * 12) + " months old!");
input.nextLine(); // Consume leftover \n [Enter] from numeric input
System.out.print("Press Enter to continue...");
input.nextLine();
// -----------------------------------------------------------------------------
System.out.println("\n--- Demo 2 - Square Area Calculator ---");
System.out.print("Enter the length of the square: ");
double length = input.nextDouble();
double area = length * length;
System.out.println("The area of the square is " + area);
input.nextLine(); // Consume leftover \n [Enter] from numeric input
System.out.print("Press Enter to continue...");
input.nextLine();
// -----------------------------------------------------------------------------
System.out.println("\n--- Demo 3 - Score Calculator ---");
System.out.print("Enter the score for game 1: ");
int score1 = input.nextInt();
System.out.print("Enter the score for game 2: ");
int score2 = input.nextInt();
System.out.print("Enter the score for game 3: ");
int score3 = input.nextInt();
int totalScore = score1 + score2 + score3;
System.out.println("The total score for the games is " + totalScore);
input.nextLine();
System.out.print("Press Enter to continue...");
input.nextLine();
//
}
}

Task 5: Shopping Cart Calculator

Program.java
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("--- Demo 1 - Age to Months Calculator ---");
System.out.print("Enter your age: ");
int age = input.nextInt();
System.out.println("You are " + age + " years old. That's " + (age * 12) + " months old!");
input.nextLine(); // Consume leftover \n [Enter] from numeric input
System.out.print("Press Enter to continue...");
input.nextLine();
// -----------------------------------------------------------------------------
System.out.println("\n--- Demo 2 - Square Area Calculator ---");
System.out.print("Enter the length of the square: ");
double length = input.nextDouble();
double area = length * length;
System.out.println("The area of the square is " + area);
input.nextLine(); // Consume leftover \n [Enter] from numeric input
System.out.print("Press Enter to continue...");
input.nextLine();
// -----------------------------------------------------------------------------
System.out.println("\n--- Demo 3 - Score Calculator ---");
System.out.print("Enter the score for game 1: ");
int score1 = input.nextInt();
System.out.print("Enter the score for game 2: ");
int score2 = input.nextInt();
System.out.print("Enter the score for game 3: ");
int score3 = input.nextInt();
int totalScore = score1 + score2 + score3;
System.out.println("The total score for the games is " + totalScore);
input.nextLine();
System.out.print("Press Enter to continue...");
input.nextLine();
// -----------------------------------------------------------------------------
System.out.println("\n--- Demo 4 - Shopping Cart Subtotal ---");
System.out.print("Enter the price of the first item: ");
double item1Price = input.nextDouble();
System.out.print("Enter the price of the second item: ");
double item2Price = input.nextDouble();
double subtotal = item1Price + item2Price;
System.out.println("The subtotal is $" + subtotal);
// Try this to force the subtotal to two decimal places.
// System.out.println(String.format("The subtotal is $%.2f", subtotal));
input.nextLine();
System.out.print("Press Enter to continue...");
input.nextLine();
//
}
}

Task 6: Hours to Minutes Calculator

Program.java
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("--- Demo 1 - Age to Months Calculator ---");
System.out.print("Enter your age: ");
int age = input.nextInt();
System.out.println("You are " + age + " years old. That's " + (age * 12) + " months old!");
input.nextLine(); // Consume leftover \n [Enter] from numeric input
System.out.print("Press Enter to continue...");
input.nextLine();
// -----------------------------------------------------------------------------
System.out.println("\n--- Demo 2 - Square Area Calculator ---");
System.out.print("Enter the length of the square: ");
double length = input.nextDouble();
double area = length * length;
System.out.println("The area of the square is " + area);
input.nextLine(); // Consume leftover \n [Enter] from numeric input
System.out.print("Press Enter to continue...");
input.nextLine();
// -----------------------------------------------------------------------------
System.out.println("\n--- Demo 3 - Score Calculator ---");
System.out.print("Enter the score for game 1: ");
int score1 = input.nextInt();
System.out.print("Enter the score for game 2: ");
int score2 = input.nextInt();
System.out.print("Enter the score for game 3: ");
int score3 = input.nextInt();
int totalScore = score1 + score2 + score3;
System.out.println("The total score for the games is " + totalScore);
input.nextLine();
System.out.print("Press Enter to continue...");
input.nextLine();
// -----------------------------------------------------------------------------
System.out.println("\n--- Demo 4 - Shopping Cart Subtotal ---");
System.out.print("Enter the price of the first item: ");
double item1Price = input.nextDouble();
System.out.print("Enter the price of the second item: ");
double item2Price = input.nextDouble();
double subtotal = item1Price + item2Price;
System.out.println("The subtotal is $" + subtotal);
// Try this to force the subtotal to two decimal places.
// System.out.println(String.format("The subtotal is $%.2f", subtotal));
input.nextLine();
System.out.print("Press Enter to continue...");
input.nextLine();
// -----------------------------------------------------------------------------
System.out.println("\n--- Demo 5 - Hours to Minutes Converter ---");
System.out.print("Enter a number of hours: ");
int hours = input.nextInt();
int minutes = hours * 60;
System.out.println(hours + " hours is equal to " + minutes + " minutes.");
//
}
}
divider

Sample Output

Your program output should something similar to the sample output below.

Sample Output
--- Demo 1 - Age to Months Calculator ---
Enter your age: 35
You are 35 years old. That's 420 months old!
Press Enter to continue...
--- Demo 2 - Square Area Calculator ---
Enter the length of the square: 12.5
The area of the square is 156.25
Press Enter to continue...
--- Demo 3 - Score Calculator ---
Enter the score for game 1: 76
Enter the score for game 2: 45
Enter the score for game 3: 32
The total score for the games is 153
Press Enter to continue...
--- Demo 4 - Shopping Cart Subtotal ---
Enter the price of the first item: 13.12
Enter the price of the second item: 14.28
The subtotal is $27.4
Press Enter to continue...
--- Demo 5 - Hours to Minutes Converter ---
Enter a number of hours: 11
11 hours is equal to 660 minutes.
divider

Reflection Questions

You may write your reflection answers as comments at the bottom of your code.

  1. In what scenarios would you choose to use the Scanner.nextInt() method versus the Scanner.nextDouble() method? Give one specific example for each.
  2. Describe a real-world application or program you could create that would require using numeric input from a user. What data would you need to collect, and what calculations would you perform?
  3. Why is it important to "consume" the leftover \n character with Scanner.nextLine() after using numeric input methods? What would happen if you forgot to include this step in a program that also needed to read a string?
divider

Submission

Submit your activity and reflection answers to the appropriate dropbox.

Activity Complete