Back

Activity 1.6 Console Input

divider

Introduction

Activity 1.6

Console Input

Topics

  • Console Input Behavior
  • Scanner
    • Importing class files
    • Initializing a Scanner object.
    • The Scanner.nextLine() method

Console Input

Console input is a process where a computer program receives data from a user through a text-based interface, such as the terminal window.

Console Input

The user is provided with a prompt, such as a question or command. They can then type their response (input) into the terminal.

Terminal window
Enter your name:

Console Input

The user is provided with a prompt, such as a question or command. They can then type their response (input) into the terminal.

Terminal window
Enter your name: Anthony Mortimer

Console Input

To submit user input, press the enter key.

Terminal window
Enter your name: Anthony Mortimer [Enter]
Hello Anthony Mortimer!

Using Scanner for Input

Scanner is a powerful Java tool that makes it easy to read input from various sources, such as the keyboard or a text file.

Demo: Initialize a Scanner Object

Step 1: Import Scanner
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
}
}

You can also import Scanner using intellisense. Follow the instructor's demo.

Demo: Initialize a Scanner Object

Step 2: Declare a Scanner Variable
Since Scanner is a Java object, its name is capitalized. The expression new Scanner(System.in) creates a new Scanner object that reads input from the keyboard.
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
}
}

This code is confusing, so take your time. We will spend an entire unit on this!

Demo: Initialize a Scanner Object

Step 3: Read User Input

The input variable, which is a Scanner object, contains a number of methods for reading keyboard input.

This activity focuses on string input using Scanner.nextLine().

Demo: Initialize a Scanner Object

Step 3: Read User Input

The input variable, which is a Scanner object, contains a number of methods for reading keyboard input.

This activity focuses on string input using Scanner.nextLine().

import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter your favorite food: ");
String food = input.nextLine();
}
}

Demo: Initialize a Scanner Object

Step 3: Read User Input

Scanner.nextLine() is a method used to read an entire line of input from the user, including any spaces. It waits for the user to type something and press the enter key, then returns everything typed on that line as a String.

import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter your favorite food: ");
String food = input.nextLine();
}
}

Demo: Initialize a Scanner Object

Step 3: Read User Input

The System.out.print() displays text, but does not go to the next line. It is commonly used when prompting the user for input.

import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter your favorite food: ");
String food = input.nextLine();
}
}

Demo: Initialize a Scanner Object

Step 3: Read User Input

Scanner.nextLine() will return any input as a string. Returned values, can be used anywhere data of that type would normally exist.

The returned value is being assigned to the food variable.

import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter your favorite food: ");
String food = input.nextLine();
}
}

Demo: Initialize a Scanner Object

Step 4: Use the input in your program
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter your favorite food: ");
String food = input.nextLine();
System.out.println("I love " + food);
}
}

Demo: Initialize a Scanner Object

Final Result

Key Terms

Console Input
A process where a computer program receives data from a user through a text-based interface
import statement
A statement used at the beginning of a file to include classes from other packages. Ex. 'java.util'.
Return Statement
A statement used to exit the method and send a value back to the code that called it.

'F' → Fullscreen

Objectives

  • icon Importing classes and initializing objects.
  • icon Prompting a user for input and saving it to a variable.
  • icon Using variables that contain user input.
divider

Activity Tasks

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

Task 1: Initialize Scanner

  • icon Import the Scanner class file.
  • icon Initialize a new Scanner object.
Program.java
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
}
}

Task 2: Simple To-Do List

  • icon Don't forget to interact with the program in the terminal!
Program.java
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String task1;
String task2;
String task3;
System.out.println("--- Demo 1 - Simple To-Do List ---");
System.out.println("Let's add a few tasks to your list.");
System.out.print("Task 1: ");
task1 = input.nextLine();
System.out.print("Task 2: ");
task2 = input.nextLine();
System.out.print("Task 3: ");
task3 = input.nextLine();
System.out.println("\nYour To-Do List:");
System.out.println("1. " + task1);
System.out.println("2. " + task2);
System.out.println("3. " + task3);
System.out.print("Press enter to continue...");
input.nextLine(); // Pause before the next demo app
//
}
}

Task 3: User Profile

Program.java
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String task1;
String task2;
String task3;
System.out.println("--- Demo 1 - Simple To-Do List ---");
System.out.println("Let's add a few tasks to your list.");
System.out.print("Task 1: ");
task1 = input.nextLine();
System.out.print("Task 2: ");
task2 = input.nextLine();
System.out.print("Task 3: ");
task3 = input.nextLine();
System.out.println("\nYour To-Do List:");
System.out.println("1. " + task1);
System.out.println("2. " + task2);
System.out.println("3. " + task3);
System.out.print("Press enter to continue...");
input.nextLine(); // Pause before the next demo app
// -----------------------------------------------------------------------------
System.out.println("\n--- Demo 2 - User Profile Setup ---");
System.out.println("WELCOME! LET'S SET UP YOUR PROFILE...");
System.out.print("Enter your username: ");
String username = input.nextLine();
System.out.print("What's your favorite subject in school? ");
String subject = input.nextLine();
// List of options
System.out.println("Choose your preferred after-school activity:");
System.out.println("- Sports");
System.out.println("- Music");
System.out.println("- Gaming");
System.out.println("- Volunteering");
System.out.print("-> "); // This is just an arrow prompt.
String activity = input.nextLine();
System.out.println("\n- Creating your profile -");
System.out.println("NAME: " + username);
System.out.println("FAVORITE SUBJECT: " + subject);
System.out.println("AFTER-SCHOOL ACTIVITY: " + activity);
System.out.print("Press enter to continue...");
input.nextLine();
//
}
}

Task 4: Simple Survey

Program.java
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String task1;
String task2;
String task3;
System.out.println("--- Demo 1 - Simple To-Do List ---");
System.out.println("Let's add a few tasks to your list.");
System.out.print("Task 1: ");
task1 = input.nextLine();
System.out.print("Task 2: ");
task2 = input.nextLine();
System.out.print("Task 3: ");
task3 = input.nextLine();
System.out.println("\nYour To-Do List:");
System.out.println("1. " + task1);
System.out.println("2. " + task2);
System.out.println("3. " + task3);
System.out.print("Press enter to continue...");
input.nextLine(); // Pause before the next demo app
// -----------------------------------------------------------------------------
System.out.println("\n--- Demo 2 - User Profile Setup ---");
System.out.println("WELCOME! LET'S SET UP YOUR PROFILE...");
System.out.print("Enter your username: ");
String username = input.nextLine();
System.out.print("What's your favorite subject in school? ");
String subject = input.nextLine();
// List of options
System.out.println("Choose your preferred after-school activity:");
System.out.println("- Sports");
System.out.println("- Music");
System.out.println("- Gaming");
System.out.println("- Volunteering");
System.out.print("-> "); // This is just an arrow prompt.
String activity = input.nextLine();
System.out.println("\n- Creating your profile -");
System.out.println("NAME: " + username);
System.out.println("FAVORITE SUBJECT: " + subject);
System.out.println("AFTER-SCHOOL ACTIVITY: " + activity);
System.out.print("Press enter to continue...");
input.nextLine();
// -----------------------------------------------------------------------------
System.out.println("\n--- Demo 3 - Simple Survey ---");
System.out.println("We'd love to get your feedback!");
System.out.print("What is your favorite color? ");
String favoriteColor = input.nextLine();
System.out.print("What is your favorite food? ");
String favoriteFood = input.nextLine();
System.out.print("Where is a place you would like to travel? ");
String travelDestination = input.nextLine();
System.out.println("\n- Survey Results -");
System.out.println("Favorite Color: " + favoriteColor);
System.out.println("Favorite Food: " + favoriteFood);
System.out.println("Travel Destination: " + travelDestination);
System.out.print("Press enter to continue...");
input.nextLine();
//
}
}

Task 5: Fantasy Story Builder

Program.java
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String task1;
String task2;
String task3;
System.out.println("--- Demo 1 - Simple To-Do List ---");
System.out.println("Let's add a few tasks to your list.");
System.out.print("Task 1: ");
task1 = input.nextLine();
System.out.print("Task 2: ");
task2 = input.nextLine();
System.out.print("Task 3: ");
task3 = input.nextLine();
System.out.println("\nYour To-Do List:");
System.out.println("1. " + task1);
System.out.println("2. " + task2);
System.out.println("3. " + task3);
System.out.print("Press enter to continue...");
input.nextLine(); // Pause before the next demo app
// -----------------------------------------------------------------------------
System.out.println("\n--- Demo 2 - User Profile Setup ---");
System.out.println("WELCOME! LET'S SET UP YOUR PROFILE...");
System.out.print("Enter your username: ");
String username = input.nextLine();
System.out.print("What's your favorite subject in school? ");
String subject = input.nextLine();
// List of options
System.out.println("Choose your preferred after-school activity:");
System.out.println("- Sports");
System.out.println("- Music");
System.out.println("- Gaming");
System.out.println("- Volunteering");
System.out.print("-> "); // This is just an arrow prompt.
String activity = input.nextLine();
System.out.println("\n- Creating your profile -");
System.out.println("NAME: " + username);
System.out.println("FAVORITE SUBJECT: " + subject);
System.out.println("AFTER-SCHOOL ACTIVITY: " + activity);
System.out.print("Press enter to continue...");
input.nextLine();
// -----------------------------------------------------------------------------
System.out.println("\n--- Demo 3 - Simple Survey ---");
System.out.println("We'd love to get your feedback!");
System.out.print("What is your favorite color? ");
String favoriteColor = input.nextLine();
System.out.print("What is your favorite food? ");
String favoriteFood = input.nextLine();
System.out.print("Where is a place you would like to travel? ");
String travelDestination = input.nextLine();
System.out.println("\n- Survey Results -");
System.out.println("Favorite Color: " + favoriteColor);
System.out.println("Favorite Food: " + favoriteFood);
System.out.println("Travel Destination: " + travelDestination);
System.out.print("Press enter to continue...");
input.nextLine();
// -----------------------------------------------------------------------------
System.out.println("\n--- Demo 4 - Fantasy Story Builder ---");
System.out.println("Let's create a fantasy story!");
System.out.print("Name your main character: ");
String mainCharacter = input.nextLine();
System.out.print("Name a magical creature: ");
String magicalCreature = input.nextLine();
System.out.print("Name a magical place: ");
String magicalPlace = input.nextLine();
System.out.println("\n- Your Story -");
System.out.println("Once upon a time, there was a hero named " + mainCharacter + ".");
System.out.println(mainCharacter + " traveled to the " + magicalPlace + " to seek the legendary " + magicalCreature + ".");
System.out.println("Their quest was just beginning...");
//
}
}
divider

Sample Output

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

Sample Output
--- Demo 1 - Simple To-Do List ---
Let's add a few tasks to your list.
Task 1: Do Homework
Task 2: Cook Dinner
Task 3: Clean Bedroom
Your To-Do List:
1.Do Homework
2.Cook Dinner
3.Clean Bedroom
Press enter to continue...
--- Demo 2 - User Profile Setup ---
WELCOME! LET'S SET UP YOUR PROFILE...
Enter your username: amortimer
What's your favorite subject in school? Computer Science
Choose your preferred after-school activity:
- Sports
- Music
- Gaming
- Volunteering
-> Gaming
- Creating your profile -
NAME: amortimer
FAVORITE SUBJECT: Computer Science
AFTER-SCHOOL ACTIVITY: Gaming
Press enter to continue...
--- Demo 3 - Simple Survey ---
We'd love to get your feedback!
What is your favorite color? purple
What is your favorite food? Pizza Hut Pepperoni Pizza
Where is a place you would like to travel? Great Britain
- Survey Results -
Favorite Color: purple
Favorite Food: Pizza Hut Pepperoni Pizza
Travel Destination: Great Britain
Press enter to continue...
--- Demo 4 - Fantasy Story Builder ---
Let's create a fantasy story!
Name your main character: Fart Knight
Name a magical creature: Logan Paul
Name a magical place: Cleveland, Ohio
- Your Story -
Once upon a time, there was a hero named Fart Knight.
Fart Knight traveled to the Cleveland, Ohio to seek the legendary Logan Paul.
Their quest was just beginning...
divider

Reflection Questions

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

    1. How did the Scanner.nextLine() method allow you to make your programs interactive?
    2. Why is it important to store user input in a variable before you can use it?
divider

Submission

Submit your activity and reflection answers to the appropriate dropbox.

Activity Complete