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)

MethodDescription
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.in);

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= scan.next().charAt(0); //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:

Example:

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);
System.out.println(“”);
int a=10, b=20;

System.out.println(a*b);//200
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);
System.out.println(“”);

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

}
}


Java Language Syllabus

Java Videos

Follow me on social media:

2 thoughts on “Reading User Input”

Comments are closed.