User Input

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

To get user input in java we must create a Scanner object. In order to use the scanner you must add the line import java.util.Scanner; to the top of your file. We can then use that object to read input from the user as many times as we want. YOU ONLY NEED TO CREATE ONE SCANNER OBJECT IT CAN BE USED TO GET INPUT MULTIPLE TIMES

import java.util.Scanner;

public class ScannerExample
{
    public static void main (String[] args)
    {
        //Create a scanner object
        Scanner scan = new Scanner(System.in);
        String name;

        System.out.println("Enter your name:");
        //get and store the student name
        name = scan.nextLine();

        System.out.println(name + " is probably like the best name ever!");
    }
}

Input of Strings

To read the user's input as a string, we usescan.nextLine(). This does not create a prompt for the user it only gets a value. You should use a print statement before getting input so the user knows what to enter. 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 NOTE: YOU ONLY NEED TO CREATE ONE SCANNER OBJECT IT CAN BE USED TO GET INPUT MULTIPLE TIMES:

//Create a scanner object
        Scanner scan = new Scanner(System.in);
        String name;

        System.out.println("Enter your name:");
        //get and store the student name
        name = scan.nextLine();

        System.out.println(name + " is probably like the best name ever!");

Input of Integers

To read the user's input in an integer format, we use.nextInt(). This allows us to ask the user for their age, favorite number, or any other whole number. NOTE: if you are going to be reading lines after integers, you must put in make a call to .nextLine() once before you try to get the line from the user. This is because nextInt() Leaves the new line character from the previous input so we must consume it.

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

//Create a scanner object
        Scanner scan = new Scanner(System.in);
        int age;

        System.out.println("Enter your age:");
        //get and store the student name
        age = scan.nextInt();

        System.out.println(age);

Input of Doubles

To read the user's input as a double, we use.nextDouble(). 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:

//Create a scanner object
        Scanner scan = new Scanner(System.in);
        double miles;

        System.out.println("Enter miles traveled:");
        //get and store the student name
        miles = scan.nextDouble();

        System.out.println(miles);

Input of Booleans

To read the user's input as a boolean, we use.nextBoolean(). The user must answer true or false.

Here is an example of how we would ask the user for how if you have pets, and then print the input:

//Create a scanner object
        Scanner scan = new Scanner(System.in);
        boolean pets;

        System.out.println("Do you have pets(true/false): ");
        //get and store the student name
        pets = scan.nextBoolean();

        System.out.println(pets);

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:

import java.util.Scanner;

public class UserInput
{
    public static void main(String[] args)
    {
        Scanner reader = new Scanner(System.in);

        System.out.println("What is your name? ");
        String name = reader.nextLine();
        System.out.println("What is your age? ");
        int age = reader.nextInt("How old are you? ");
        System.out.println("Do you have any pets(true/false)? ");
        boolean = reader.nextBoolean("Pets? ");

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

Here is what the user prompts will look like, once the code executes: "User Input Example"

results matching ""

    No results matching ""