Java Program to get Input from User

Input from the user can be taken using the Scanner Class in Java.

Java Scanner Class

Java Scanner Class enables programs to take inputs from users from the console. The package that contains the Scanner class is the java.util package. This is used to read and interpret inputs of primitive data types like int, double, long, short, float. and byte. The Scanner class is also one of the simplest ways to perform input reads into a program from the console. All you need to do is make the necessary import and call an object of the Scanner class.

Syntax to Create a Constructor of the Scanner Class

Scanner sc = new Scanner (System.in)

This creates a new constructor of the Scanner class and the argument that is passed to the constructor is ‘System.in’. This signifies that the scanner object ‘sc’ is going to read from the standard input stream of the program. The java.util package needs to be imported right at the start of the program when you want to use the Scanner class. It also makes sure that Bytes (from the input stream data) are converted into characters using the platform’s default charset.

Methods of the Java Scanner Class

This is a list of the methods that the Scanner class offers to enable programs to read different primitive types.

MethodDescription
Int nextInt()It is used to scan the next token of the input as an integer.
Float nextFloat()It is used to scan the next token of the input as a float.
Double nextDouble()It is used to scan the next token of the input as a double.
Byte nextByte()It is used to scan the next token of the input as a byte.
String nextLine()Takes the scanner to the next line of input provided.
Boolean nextBoolean()It is used to scan the next token of the input into a boolean value that either takes true or false.
Long nextLong()It is used to scan the next token of the input as a long.
Short nextShort()It is used to scan the next token of the input as a Short.
BigInteger nextBigInteger()It is used to scan the next token of the input as a BigInteger.
BigDecimal nextBigDecimal()It is used to scan the next token of the input as a Big Decimal.

 

Sample Code

The code presented below illustrates how to take input from the console into a Java program using the Scanner class.

import java.util.*;
class Demo
{
public static void main (String args [])
{
Scanner sc = new Scanner(System.in);
System.out.println (“Enter the first Number”);
int a = sc.nextInt();
System.out.println (“Enter the second number”);
int b = sc.nextInt();
System.out.println (“Enter the third number”);
int c = sc.nextInt();
int d = a + b+ c;
System.out.println (“Total” + d);
}
}

 

OUTPUT

Enter first number – 6
Enter second number – 44
Enter third number – 23
Total = 73

Sample Program

The program below takes String input from the user

import java.util.*;
class Demo
{
public static void main (String args [])
{
Scanner sc = new Scanner (System.in);
System.out.println (“Enter a String”);
String str = sc.nextLine();
System.out.println (“The String you have entered is :” + str);
}
}

 

OUTPUT

Enter a String : Good Morning. Hope you have a nice day
The String you have entered is: Good Morning. Hope you have a good day

 

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.