AP Computer Science in Java
  • Introduction
  • Introduction to Programming in Java with Karel the Dog
    • Introduction to Programming with Karel
    • More Basic Karel
    • Java Programs and the Run Method
    • Karel Can't Turn Right
    • Methods in Karel
    • Top Down Design and Decomposition in Karel
    • Commenting Your Code
    • SuperKarel
    • For Loops
    • While Loops in Karel
    • If Statements
    • If/Else Statements
    • Control Structures Example
    • How To Indent Your Code
  • Basic Java
    • Printing in Java
    • Variables and Types
    • User Input
    • Arithmetic Expressions
    • Casting
    • Booleans
    • Logical Operators
    • Comparison Operators
    • For Loops
    • While Loops
    • If Statements
    • Loop-and-a-Half
    • Short-Circuit Evaluation
    • De Morgan's Laws
    • Strings
  • Methods
    • Java Methods
    • Methods and Parameters
    • Methods and Return Values
    • Javadoc and More Methods
    • Strings Methods
    • Strings and Characters
    • Exceptions
    • String Processing
  • Classes and Object-Oriented Programming
    • Introduction To Classes and Objects
    • Classes vs. Objects
    • Using a Class as a Client
    • Writing Classes
    • Writing Classes and Instance Methods
    • Getter and Setter Methods
    • Class Methods and Class Variables
    • Method Overloading
    • Local Variables and Scope
    • Key Terms for Classes
    • Objects vs Primitives
    • Inheritance
    • Class Design and Abstract Classes
    • Polymorphism
    • Interfaces
  • Data Structures
    • What Are Data Structures?
    • Introduction to Arrays
    • Using Arrays
    • ArrayList Methods
    • Arrays vs ArrayLists
    • 2D Arrays (Matrices or Grids)
    • Hashmaps
  • Algorithms and Recursion
    • What is an Algorithm?
    • Pseudocode
    • Linear Search
    • Binary Search
    • Selection Sort
    • Insertion Sort
    • Advanced: Recursion
    • Mergesort
Powered by GitBook
On this page
  • Getting User Input
  • Input of Strings
  • Input of Integers
  • Input of Doubles
  • Input of Booleans
  • User Input Applications

Was this helpful?

  1. Basic Java

User Input

PreviousVariables and TypesNextArithmetic Expressions

Last updated 5 years ago

Was this helpful?

Being able to obtain user input creates new opportunities while coding. This allows us to take in data from the user to make our programs work for them.

Getting User Input

In Java there are four ways we can obtain user input:

Input of Strings

To read the user's input as a string, we usereadLine("String prompt"). This allows us to ask the user for their name, favorite color, or any other text input from the user.

Here is an example of how we would ask the user for their favorite color, and then print it:

// Ask for the input, and store it in the 'favColor' string.
String favColor = readLine("Hello, what is your favorite color? ");
// Then print it.
System.out.println(favColor);

Input of Integers

To read the user's input in an integer format, we usereadInt("String prompt"). This allows us to ask the user for their age, favorite number, or any other whole number.

Here is an example of how we would ask the user for their age, and then print it:

// Ask for the input, and store it in the 'age' integer.
int age = readInt("Hello, how old are you? ");
// Then print it.
System.out.println(age);

Input of Doubles

To read the user's input as a double, we usereadDouble("String prompt"). This allows us to ask the user how many miles they have traveled, how much an item costs, or any other numerical value that has a decimal.

Here is an example of how we would ask the user for how many miles they have traveled, and then print the input:

// Ask for the input, and store it in the 'miles' double.
double miles = readDouble("How many miles have you traveled? ");
// Then print it.
System.out.println(miles);

Input of Booleans

To read the user's input as a boolean, we usereadBoolean("String prompt"). This allows us to ask the user any true or false question.

Here is an example of how we would ask the user if they are a astronaut, and then print their answer:

// Ask for the input, and store it in the 'isAstronaut' boolean.
boolean isAstronaut = readBoolean("Hello, are you an astronaut? ");
// Then print it.
System.out.println(isAstronaut);

User Input Applications

Now that we have seen a some examples of how to get user input, lets look an application.

Lets say we are writing a piece of code in which we want to ask the user for their name, age, and if they own a pet. In this case we will ask the user each question, one at a time, and store the answers in their own variables. After we get the user input we will print the variables. Here is what our code will look like:

public class UserInput extends ConsoleProgram
{
    public void run()
    {
        String name = readLine("What is your name? ");
        int age = readInt("How old are you? ");
        boolean hasPet = readBoolean("Do you own a pet? ");

        System.out.println("");
        System.out.println("Your name is " + name);
        System.out.println("You are " + age + " years old.");
        System.out.println("You own a pet: " + hasPet);
    }
}

Here is what the user prompts will look like, once the code executes:

User Input Table
User Input Example