Reading User Input

Reading User Input, Java User Input, Java Output, Java Scanner Class, Java System Class, Java File Handling, and Java Database Connectivity.

Reading User Input in Java

1. Reading User Input

The Scanner class is used to get user input and it is available in java.util package

To use the Scanner class, create an object of the class, using any available methods perform reading operations

Input Types (Java Scanner class/object methods to read different types of user input)

1. nextLine()It reads a String value from the User
2. nextByte()It reads a byte value from the User
3. nextShort()It reads a short value from the User
4. nextInt()It reads an int value from the User
5. nextLong()It reads a long value from the User
6. nextFloat()It reads a float value from the User
7. nextDouble()It reads a double value from the User
8. nextBoolean()It reads a boolean value from the User
9. next().charAt(index)It reads a character value from the User

Syntax to create an object in Java class:

ClassName(Predefined/User defined) objectName = new ClassName(Predefined/User defined) (arg);

Java Example to read different types of input using Scanner class/object:

package package1;

import java.util.Scanner;

public class Sample {

public static void main(String[] args) {
//Create an object using Scanner Class
Scanner scan = new Scanner(;

System.out.println(“Enter Your Name: “);
String name = scan.nextLine(); //Read String type input
System.out.println(“Your Name is: “+name);

System.out.println(“Enter a Number”);
int num= scan.nextInt(); //Read Integer type input
System.out.println(“Your Number is: “+num);

System.out.println(“Enter your Mobile Number: “);
long mobileNum = scan.nextLong(); //Read Long type input
System.out.println(“Your Mobile Number is: “+mobileNum);

System.out.println(“Enter a Value: “);
float x= scan.nextFloat(); // Read Float type input
System.out.println(“Your Number is: “+x);

System.out.println(“Enter a Value: “);
double y= scan.nextDouble(); //Read Double type input
System.out.println(“Your Number is: “+y);

System.out.println(“Enter a Logical Value: “);
boolean z= scan.nextBoolean(); //Read Boolean type input
System.out.println(“Value is: “+z);

System.out.println(“Enter a Character: “);
char a=; //Read Character type input
System.out.println(“Character is: “+a);


2. Writing Java Output

Writing Program output on the Console

Using System class we can write output statements in Java:


public class Sample {
public void comparison(int num1, int num2) {
if (num1>num2) {
System.out.println(“Num1 is Big Number”);
else {
System.out.println(“Num2 is Big Numer”);

public static void main(String[] args) {

Sample obj = new Sample();
obj.comparison(989898, 986543);
int a=10, b=20;

System.out.println(“Hello Java World”);
System.out.println(“Addition of a and b is: “+ (a+b));
System.out.println(“a value is: “+a+”, “+”b value is: “+b);

if (a>b)
System.out.println(“A is Big Number”);
System.out.println(“B is Big Number”);
for (int i=1; i<=3; i++){


Java Language Syllabus

Java Videos

Follow me on social media: