Getting input from the user

Updated: 3 years ago

Check out other lessons in Learning the Java syntax

Until​ now we have always hardcoded our values. Now it's time to ask the user for input and then handle that input inside our application.

Scanner

We'll use the Scanner class to read input from the user. Scanner is a very useful class that helps us to get primitive values and strings from keyboard keypresses.

package com.codescrubs;

// We need to tell the compiler from where to import the Scanner class
import java.util.Scanner;

public class Main
{
    public static void main(String[] args) 
    {
          // Creating a new Scanner object that we will use to read input
          Scanner scanner = new Scanner(System.in);
    }
}

At the top of our code you can see the import java.util.Scanner statement which is very important. We need to tell the compiler to import the Scanner class from a specific location and if we forget that, our code won't work. Make sure that you have that import statement.

Then we declare a variable of type Scanner and we name it scanner. Then we create a new Scanner object and we'll pass in System.in. We use System.out to write text to the screen, we use System.in to get input.

I know that it sounds a bit complicated when we're talking about classes and methods, but bare with me a couple of more lessons until we get to them.

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

          System.out.println("Tell me your name:");

          // Let's read the input with scanner and store the value to a name variable
          String name = scanner.nextLine();

          System.out.println("Your name is " + name);   
    }

We use nextLine() to read a line of input from the user. Every time we call nextLine(), we'll get back a string. We then store that string to a variable.

Imagine input as a queue. Every keypress get's stored in that queue, even hitting the enter gets stored. You use the scanner object and the nextLine() method to get that queue of keypresses as a string. The nextLine() method will automatically remove the enter keypress from the string, so you end up with a nice clean string.

nextInt() and nextFloat()

You can use scanner to retrieve different types of values from the user. We used nextLine() to get a string. You can use nextInt() or nextFloat() or even nextBoolean() to get different types of values.

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

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

          // Let's get the input as an int
          int age= scanner.nextInt();

          System.out.println("Your age is " + age);   

          // Let's clear the queue of any leftover keypresses 
          scanner.nextLine();

          System.out.println("How much money do you have?");

          // Let's get the input as a float
          float money = scanner.nextFloat();

          System.out.println("You have " + money);   

          // Let's clear the queue of any leftover keypresses 
          scanner.nextLine();
    }

You may be wondering what's with the extra scanner.nextLine()? We know that input works the same way as a queue. Well thernextLine() method helps you out by automatically clearing the enter keypress, while nextInt() and nextFloat() and all of the other next's don't. That means, that when you read the input as an int or a float, you leave the enter keypress in the queue and it then messes up all of the following inputs.

Even though we don't assign the resulting string from a scanner.nextLine() the operation still happens. It will check the queue, it will find a single enter keypress and return that. Because we don't need that, we won't assign it to a variable and we'll just let it be lost in the void of space.

« Previous lesson:
Loops
» Next lesson:
Exceptions