0% found this document useful (0 votes)
68 views8 pages

User Input in Java with Scanner

The document discusses how to take input from the user in Java using the Scanner class. The Scanner class allows input to be read from the console and belongs to the java.util package. It can be used to read primitive types like int, double, long etc. Examples are provided to demonstrate reading integer and string input from the user.

Uploaded by

RAAGA VEDA
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)
68 views8 pages

User Input in Java with Scanner

The document discusses how to take input from the user in Java using the Scanner class. The Scanner class allows input to be read from the console and belongs to the java.util package. It can be used to read primitive types like int, double, long etc. Examples are provided to demonstrate reading integer and string input from the user.

Uploaded by

RAAGA VEDA
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

How to get input from user in

Java
Java Scanner Class

• Java Scanner class allows the user to take input from the console.


• It belongs to [Link] 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.
Syntax

Scanner sc=new Scanner([Link]);  
Methods of Java Scanner Class

Method Description
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.
Example of integer input from user
[Link] [Link].*;  
[Link] UserInputDemo   
3.{  
[Link] static void main(String[] args)  
5.{  
[Link] sc= new Scanner([Link]);    //[Link] is a standard input stream  
[Link]("Enter first number- ");  
[Link] a= [Link]();  
[Link]("Enter second number- ");  
[Link] b= [Link]();  
[Link]("Enter third number- ");  
[Link] c= [Link]();  
[Link] d=a+b+c;  
[Link]("Total= " +d);  
15.}  
16.}  
Example of String Input from user
[Link] [Link].*;  
[Link] UserInputDemo1  
3.{  
[Link] static void main(String[] args)  
5.{  
[Link] sc= new Scanner([Link]); //[Link] is a standard input stream  
[Link]("Enter a string: ");  
[Link] str= [Link]();              //reads string  
[Link]("You have entered: "+str);             
10.}  
11.}  

You might also like