← All AP Java lessons

AP Java / J03

User Input

Using Scanner, collecting user values, processing input, and five complete assignments.

Java - Intro 3. User Input

3. User Input

3.1 Overview

3.2 The Big Picture

Chapter 3: Assignments

A3.1 Gather Basic Information

A3.2 Adding Machine

A3.3 Short Mad-Lib

A3.4 Volume and Surface Area of a Rectangular Prism

A3.5 Adding and Multiplying User Input

3. User Input

3.1 Overview

3.2 The Big Picture

Sample Program:

import java.util.Scanner;

public class HelloWorld

{

public static void main(String[] arg)

{

int age;

// In the line below, “userInput” can be anything you want

Scanner userInput = new Scanner(System.in);

System.out.print("How old are you?");

age = userInput.nextInt();

System.out.println("You are a " + age + " year old!!");

userInput.close();

}

}

Sample run
How old are you? 35
You are a 35 year old!!

Chapter 3: Assignments

A3.1 Gather Basic Information

Create a program that asks a user for their first name, last name and age. Each of these values should be assigned to a different variable.

Program structure
import java.util.Scanner;
public class Prog33
{
    public static void main(String[ ] arg)
    {
        // Declare a String called first but don't initialize it
        // Declare a String called last but don't initialize it
        // Declare an int called age but don't initialize it
        Scanner info = new Scanner(System.in);
        // Create a print statement asking for a first name
        first = info.next();
        // Create a print statement asking for a last name
        last = info.next();
        // Create a print statement asking for the age
        age = info.nextInt();
        // Print the desired output
        info.close();
    }
}
Sample run
What is your first name?
Peter
What is your last name?
Bigelow
How old are you?
32
Peter Bigelow is 32 years old!

A3.2 Adding Machine

Take the user’s name and three doubles. Add together the numbers and print out the sum.

Program structure
import java.util.Scanner;
public class Prog34
{
    public static void main(String[ ] arg)
    {
        // Declare a String and three doubles
        // Create the Scanner with the input variable
        // Ask the user for their name and three numbers and assign the input to
        the proper variables.
        // Print the desired output
        // Close the Scanner
    }
}
Sample run
What is your name?
Betty
Please give me your first number:
7.2
Please give me your second number:
1.3
Please give me your third number:
10.3
Betty, the sum of your numbers is: 18.8

A3.3 Short Mad-Lib

Ask the user for at least two nouns, two verbs, two adjectives and two adverbs. They should be stored with the following variable names: noun1, noun2, verb1, verb2, adj1, adj2, adv1, adv2. Then have the program print a story to the screen based on these words.

Program structure
import java.util.Scanner;
public class Prog35
{
    public static void main(String[ ] arg)
    {
        // Declare all the String variables
        // Create the Scanner with the input variable
        // Ask the user for all of the words
        // Print the desired output
        // Close the Scanner
    }
}
Sample run
Please give me a noun:      house
Another noun:           key
Please give me a verb:      stick
Another verb (past tense):      kicked
Please give me an adjective:    fantastic
Another adjective:          deathly
Please give me an adverb:       quickly
Another adverb:         smoothly
Here is the story that you generated:
Once there was a fantastic house. It liked to stick quickly. Yesterday, the house
smoothly kicked a deathly key.

A3.4 Volume and Surface Area of a Rectangular Prism

Ask the user for three dimensions of a rectangular prism, the height, width and length as well as the unit of the measurements. The volume and the surface area should each be calculated. The program should run like this:

Program structure
import java.util.Scanner;
public class Prog36
{
    public static void main(String[ ] arg)
    {
        // Declare all variables with appropriate names
        // Create the Scanner with the input variable
        // Ask the user for values for all variables
        // Print the desired output
        // Close the Scanner
    }
}
Sample run
What is the height of the prism?
5
What is the length of the prism?
7
What is the width of the prism?
2
What units are the length measured in?
feet
The volume of the prism is 70 feet cubed and the surface area is 118 feet squared.

A3.5 Adding and Multiplying User Input

Practice collecting more information from the user, processing it, and returning information.

Program structure
import java.util.Scanner;
public class Prog37
{
    public static void main(String[ ] arg)
    {
        // See the program structure from 3.5
    }
}
Sample run
Hello! What is your name?
Jen
Hi Jen. Please give me a number:
14
Thank you. Please give me another number:
10
OK.
If I add those numbers together, I get 24.
If I multiply those numbers together, I get 140.