0% found this document useful (0 votes)
1 views

Java UserInput

The document explains how to take user input in Java using the Scanner class and lists various methods for reading different data types. It also covers identifiers, literals, reserved keywords, and type casting in Java, including widening and narrowing casting. Additionally, it provides examples and tasks for practicing user input and type conversion.

Uploaded by

lsrinivas.rpa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Java UserInput

The document explains how to take user input in Java using the Scanner class and lists various methods for reading different data types. It also covers identifiers, literals, reserved keywords, and type casting in Java, including widening and narrowing casting. Additionally, it provides examples and tasks for practicing user input and type conversion.

Uploaded by

lsrinivas.rpa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

How to take user input:

Scanner input = new Scanner(System.in);

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

Method Description

nextBoolean
() Reads a boolean value from the user

nextByte() Reads a byte value from the user


nextDouble(
) Reads a double value from the user

nextFloat() Reads a float value from the user

nextInt() Reads a int value from the user

nextLine() Reads a String value from the user

nextLong() Reads a long value from the user

nextShort() Reads a short value from the user

Example:

import java.util.Scanner;

class Main {

public static void main(String[] args) {

Scanner myObj = new Scanner(System.in);

System.out.println("Enter name, age and salary:");

// String input

String name = myObj.nextLine();

// Numerical input

int age = myObj.nextInt();

double salary = myObj.nextDouble();


// Output input by user

System.out.println("Name: " + name);

System.out.println("Age: " + age);

System.out.println("Salary: " + salary);

Task:

1.Create a program to input name of the person and

respond with "Welcome NAME to Automation World"

2.Create a program to add two numbers.

3. Try all other user input methods.

Identifiers

All Java variables must be identified with unique names.

These unique names are called identifiers.

Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume).

Note: It is recommended to use descriptive names in order to create understandable and


maintainable code:

Literals
In Java, literals are the constant values that appear directly in the program. It can be assigned
directly to a variable. Java has various types of literals. The following figure represents a literal.

Types of Literals in Java

There are the majorly four types of literals in Java:

1. Integer Literal – > Eg: 10, 5, 20, -8, -15 etc.

2. Character Literal – > Eg: ‘a’, ‘c’, ‘Z’, ‘L’ etc.

3. Boolean Literal – > Eg: true, false

4. String Literal– > Eg: “hi”, “hello”, “welcome” etc.

Reserved keywords in Java

Java has a set of reserved words that have a special meaning to the compiler and cannot be used as
identifiers (such as variable names, class names, etc.). Here's a list of the reserved keywords in Java:

abstract assert boolean break byte

case catch char class const

continue default do double else

enum extends final finally float

for goto if implements import

instanceof int interface long native

new package private protected public

return short static strictfp super

switch synchronized this throw throws

transient try void volatile while


Escape Sequences:
Type Conversion and Casting
Type casting is when you assign a value of one primitive data type to another type.

 Widening Casting (automatically) - converting a smaller type to a larger type size


byte -> short -> char -> int -> long -> float -> double

 Narrowing Casting (manually) - converting a larger type to a smaller size type


double -> float -> long -> int -> char -> short -> byte

Widening Casting (Implicit):

Widening casting is done automatically when passing a smaller size type to a larger size type.

 Conversion from a smaller data type to a larger data type is allowed.


 No explicit notation is required.
 No risk of data loss.

int myInt = 9;

double myDouble = myInt; // Automatic casting: int to double

Narrowing Casting (Explicit):

Narrowing casting must be done manually by placing the type in parentheses () in front of the value.

 Requires explicit notation using parentheses and casting.


 Conversion from a larger data type to a smaller data type is allowed.
 Risk of data loss due to truncation.
double myDouble = 9.78d;

int myInt = (int) myDouble; // Manual casting: double to int

Realtime ex:

int maxScore = 500;

int userScore = 423;

float percentage = (float) userScore / maxScore * 100;

System.out.println("User's percentage is " + percentage);

You might also like