Welcome to KLUBT. This site is for the usage of K L University Students for the complete references for materials and day to day updates of University.

Java Tutorial - Read Input From Users (with examples)

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
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

No comments:

Post a Comment