Read User Input in Java

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

Read 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 Small Number”);
byte smallNum = scan.nextByte(); //Read Byte type input
System.out.println(“Your Number is: “+smallNum);

System.out.println(“Enter a Medium Number: “);
short mediumNum= scan.nextShort(); // Read Short type input
System.out.println(mediumNum);

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

Java Videos

Java Programming Language Syllabus
——————————————
1. Introduction to Java Programming

2. Java Environment Setup

3. Java Keywords and Identifiers

4. Java Basic Syntax

5. Java Program Structure.

6. Comments in Java

7. Modifiers in Java.

8. Java Data Types.

9. Variables in Java.

10. Operators in Java

11. Java Decision Making Statements

12. Java Loop Statements

13. Java Branching Statements

14. Java Strings

15. Java Arrays

16. Java ArrayList

17. Read User Input

18. File Handing/File Operations

19. Java User-Defined Methods

20. Java Built-in Methods

21. Exception Handling in Java

22. Java Object-Oriented Programming

23. Java Inheritance

24. Polymorphism in Java

25. Abstraction in Java

Types of Output in Java Programming

Follow me on social media: