[go: up one dir, main page]

0% found this document useful (0 votes)
37 views5 pages

Input in Java

The document explains the Java Scanner class, which is part of the java.util package and is used for taking input from the console. It details various methods for reading different primitive types, such as int, float, double, and String. Additionally, it provides example code snippets demonstrating how to use the Scanner class to read numbers and strings from user input.

Uploaded by

Dipendra Km
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views5 pages

Input in Java

The document explains the Java Scanner class, which is part of the java.util package and is used for taking input from the console. It details various methods for reading different primitive types, such as int, float, double, and String. Additionally, it provides example code snippets demonstrating how to use the Scanner class to read numbers and strings from user input.

Uploaded by

Dipendra Km
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 5

INPUT IN JAVA

Java Scanner Class

Java Scanner class allows the user to take input from the console. It belongs
to java.util package. It is used to read the input of primitive types like int, double, long,
short, float, and byte. It is the easiest way to read input in Java program.
Scanner sc=new Scanner(System.in);  
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() Advances this scanner past the current line.

boolean nextBoolean() It is used to scan the next token of the input into a
boolean value.

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


BigDecimal.
import java.util.*;
class UserInputDemo
{
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in); //System.in is a standard input stream
System.out.print("Enter first number- ");
int a= sc.nextInt();
System.out.print("Enter second number- ");
int b= sc.nextInt();
System.out.print("Enter third number- ");
int c= sc.nextInt();
int d=a+b+c;
System.out.println("Total= " +d);
}
}
Input string
import java.util.*;
class UserInputDemo1
{
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in); //System.in is a standard input stream
System.out.print("Enter a string: ");
String str= sc.nextLine(); //reads string
System.out.print("You have entered: "+str);
}
}

You might also like