Part 13: User input in java with real time example code

JAVA

Java User Input

The Scanner class is used to get user input and is the easiest way to read input in Java program. Java Scanner class permits the user to take input from the console. The class is belongs to java.util package. It is used to read the input of primitive types like int, double, long, short, float, and byte.

Syntax

Scanner sc=new Scanner(System.in);

The above statement creates a constructor of the Scanner class having System.in as an argument. it is going to read from the standard input stream of the program taking input from the console. The java.util package should be import while using Scanner class. It also converts the Bytes that is  from the input stream into characters using the platform’s default charset.

The most widely used method is Scanner and I personally prefer it because of its simplicity and easy implementation, as well as its powerful utility to parse text into primitive data.

Advantages of Using Scanner

  • Easy to use the Scanner class.
  • Easy input of numbers (int, short, byte, float, long and double).
  • Exceptions are unchecked which is more convenient. It is up to the programmer to be civilized, and specify or catch the exceptions.
  • Is able to read lines, white spaces, and regex-delimited tokens

In order to use the Scanner class, create an object of the class and use any of the available methods found in the Scanner class documentation. In our example, we will use the nextLine() method, which is used to read Strings:

Example

import java.util.Scanner;  // This statement is Import the Scanner class
class ExampleMain {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);  // This statement is Create a Scanner object
System.out.println("Enter username");
String userName = myObj.nextLine();  // This statement is Read user input
System.out.println("Username is: " + userName);  // This statement is Output user input
}
}

 

OUTPUT

Enter username
Harun
Username is: Harun

If you are not well known what a package is, read our Java Packages/API Tutorial.

Input Types

In the example above, we used the nextLine() method, which is used to read Strings. To read other types, look at the table below:

Method               Description
nextBoolean() It is used to scan Reads a Boolean value from the user
nextByte() It is used to scan Reads a byte value from the user
nextDouble() It is used to scan Reads a double value from the user
nextFloat() It is used to scan Reads a float value from the user
nextInt() It is used to scan Reads an int value from the user
nextLine() It is used to scan Reads a String value from the user
nextLong() It is used to scan Reads a long value from  the user
nextShort() It is used to scan Reads a short value from the user
int nextInt() It is used to scan the next token of the input as an integer.
nextBigInteger() It is used to scan the next token of the input as a BigInteger.
nextBigDecimal() It is used to scan the next token of the input as a BigDecimal.

In the example below, we use different methods to read data of various types:

Example

import java.util.Scanner;
class ExampleMain {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
System.out.println("Enter name age and salary:");
String name = myObj.nextLine(); // This statement is String input
int age = myObj.nextInt(); // This statement is Numerical input
double salary = myObj.nextDouble();
System.out.println("Name: " + name); // This statement is Output input by user
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
}
}

 

OUTPUT

Enter name, age and salary:

Harun
22
40000

Name: Harun
Age: 22
Salary: 40000

Example of integer input from user. The following example allows user to read an integer form the System.in.

import java.util.*;
class ExampleMain
{
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in);    //System.in is a standard input stream
System.out.print("Enter first number: ");
int a= sc.nextInt();
System.out.print("Enter second number:");
int b= sc.nextInt();
System.out.print("Enter third number: ");
int c= sc.nextInt();
int d=a+b+c;
System.out.println("Total= " +d);
}
}

 

Output:

How to get input from user in Java

Example of String Input from user

Let’s see another example, in which we have taken string input.

import java.util.*;
class ExampleMain
{
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in); //System.in is a standard input stream
System.out.print("Enter a string: ");
String str= sc.nextLine();              //This statement is read string
System.out.print("You have entered: "+str);
}
}

 

Output:

How to get input from user in Java. If you enter wrong input (for example, text in a numerical input), you will get an exception/error message ( such as “InputMismatchException”).

0 Comments

You may find interest following article