JavaDevNotes.com

Java Tutorial - Read Input From Console

This tutorial will show how to read user input when creating a console application. This is very useful when the program you are writing needs to get input from user before it can proceed with processing.

System.console().readLine()

The simplest way to read user input is by using System.console(). Here is an example:

public class Sample {
  public static void main(String[] args) {
    System.out.print("Please enter your name: ");
    String name = System.console().readLine();
    System.out.println("Your name is: " + name);
  }
}

And this is a sample conversation with the user when the program is run:

Please enter your name: John
Your name is: John

The problem with using this method is that it will not run properly using an IDE (E.g. Eclipse). Here is a screenshot when you try to run this inside Eclipse:
zcona
If you wish to use this method, you can run this successfully using a command line. Here is an example:
zconb
An Eclipse project typically has src and bin subfolder. The former contains the source code (E.g. Test.java) while the latter contains the compiled .class files (E.g. Test.class). Just go to the bin folder and execute
java Sample
Where Sample is the name of the class you declared.

java.util.Scanner and System.in

Combining System.in and java.util.Scanner provides a way to read user input that can run inside an IDE. It also provides a way to read different data types.
  • read string input:
    import java.util.Scanner;
    public class Test {
      public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("What is your favorite color? ");
        String name = scanner.next();
        System.out.println("Your favorite color is: " + name);
      }
    }
    Sample output:
    What is your favorite color? blue
    Your favorite color is: blue
  • read byte input:
    import java.util.Scanner;
    public class Test {
      public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a small number: ");
        byte number = scanner.nextByte();
        System.out.println("The number is: " + number);
      }
    }
    Sample output:
    Enter a small number: 5
    The number is: 5
  • read short input:
    import java.util.Scanner;
    public class Test {
      public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a short integer: ");
        short number = scanner.nextShort();
        System.out.println("The number is: " + number);
      }
    }
    Sample output:
    Enter a short integer: 1000
    The number is: 1000
  • read int input:
    import java.util.Scanner;
    public class Test {
      public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter an integer: ");
        int number = scanner.nextInt();
        System.out.println("The number is: " + number);
      }
    }
    Sample output:
    Enter an integer: 211555777
    The number is: 211555777
  • read long input:
    import java.util.Scanner;
    public class Test {
      public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a long number: ");
        long number = scanner.nextLong();
        System.out.println("The number is: " + number);
      }
    }
    Sample output:
    Enter a long number: 12345678912
    The number is: 12345678912
  • read float input:
    import java.util.Scanner;
    public class Test {
      public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a float number: ");
        float number = scanner.nextFloat();
        System.out.println("The number is: " + number);
      }
    }
    Sample output:
    Enter a float number: 1.25
    The number is: 1.25
  • read double input:
    import java.util.Scanner;
    public class Test {
      public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a double number: ");
        double number = scanner.nextDouble();
        System.out.println("The number is: " + number);
      }
    }
    Sample output:
    Enter a float number: 55.11
    The number is: 55.11
  • read boolean input:
    import java.util.Scanner;
    public class Test {
      public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a boolean value (true or false): ");
        boolean bool = scanner.nextBoolean();
        System.out.println("You entered: " + bool);
      }
    }
    Sample output:
    Enter a boolean value (true or false): true
    You entered: true

BufferedReader, InputStreamReader, and System.in

Here is another example of getting user input using BufferedReader, InputStreamReader, and System.in. This way will also work inside an IDE.

Example code:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test {
  public static void main(String[] args) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Please enter your name? ");
    String name = reader.readLine();
    System.out.println("Your name is: " + name);
  }
}

And the behavior is the same as the samples above:

Please enter your name? James
Your name is: James

Notes

For future tutorials, we will use Scanner as it can read many different data types.
Tags: beginner tutorial, console