Tech With Tim Logo
Go back

Input and Scanners

Creating A Scanner

To get user input we need to use something called a scanner. A scanner is a data-type/object just like Strings and Ints. So to create a new scanner object we need to define Scanner as our type then give it a variable name.

However, this scanner object needs to be imported before we can use it. This means we need to type the following at the top of our program.

import java.util.Scanner;

Now to setup our scanner object we will do the following:

import java.util.Scanner;

public class Main {

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        // Now we have a scanner object (sc) that we can use to read input from the system (System.in)
    }
}

Using a Scanner

Now that we have created a scanner object we can use it to get input from the user. However, the way we get input will be different based on what type what are expecting. For example if we would like the user to type a number we will use different code than if we are expecting a String.

Getting String input from the user we do the following.

import java.util.Scanner;

public class Main {

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String scanned = sc.next();  // Gets string input

        System.out.println("You typed: " + scanned); // This will print what the user typed in
    }
}

To get int input from the user we do the following.

import java.util.Scanner;

public class Main {

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int scanned = sc.nextInt();  // Gets int input

        System.out.println("You typed: " + scanned); // This will print what the user typed in
    }
}

For boolean input from the user we do the following.

import java.util.Scanner;

public class Main {

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        boolean scanned = sc.nextBoolean();  // Gets int input

        System.out.println("You typed: " + scanned); // This will print what the user typed in
    }
}

Finally to get double input from the user we do the following.

import java.util.Scanner;

public class Main {

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        double scanned = sc.nextDouble();  // Gets int input

        System.out.println("You typed: " + scanned); // This will print what the user typed in
    }
}

Best Practice

You may have noticed that if you try to input data that is an invalid type that you run into an error. The easiest way to work around this error is to always read in information as a string and then convert it to the type you'd like.

To convert a String value to an Integer value we can use Integer.parseInt().

import java.util.Scanner;
import java.lang.*; //We need to import this to use Integer

public class Main {

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String scanned = sc.next();  
        int x = Integer.parseInt(scanned);

        System.out.println("You typed: " + scanned); // This will print what the user typed in
    }
}
Design & Development by Ibezio Logo