The document explains the Scanner class in Java, which is part of the util package and is used to obtain user input at runtime. It details various methods of the Scanner class for different data types, including int, long, byte, short, float, double, boolean, char, and String. Additionally, it provides example programs demonstrating the usage of the Scanner class and highlights the difference between the next() and nextLine() methods.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
5 views6 pages
16 Java Scanner PDF
The document explains the Scanner class in Java, which is part of the util package and is used to obtain user input at runtime. It details various methods of the Scanner class for different data types, including int, long, byte, short, float, double, boolean, char, and String. Additionally, it provides example programs demonstrating the usage of the Scanner class and highlights the difference between the next() and nextLine() methods.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6
SCANNER
Scanner is a predefined class it's part of util
package Scanner is used to get input from user we can change data value at runtime Scanner class have more methods to perform each method have an specific operations To access scanner class and scanner class methods need to import package scanner class Scanner class package are: import java.util.Scanner;
Scanner class methods based on datatypes
Example:
int is variable we can assign value int a=10;
or declare int a; In Scanner int data type using as method nextInt() such as we have all methods
Scanner class methods are:
NUMBERS:
nextLong() - to give long value input at
runtime nextByte() - to give byte value input at runtime nextShort() - to give short value input at runtime nextInt() - to give int value input at runtime
CONDITION:
nextBoolean() - to give boolean value input at
runtime DECIMAL:
nextFloat() - to give float value input at
runtime nextDouble() - to give double value input at runtime
CHARACTER:
next().charAt(indexvalue) - to give char value
input at runtime
STRING:
nextLine() - to give String value input at
runtime next() - to give String value input at runtime
Difference between next() and nextLine() if using
next() after nextInt() a String will not ask to input need to use nextLine() method
Example program for Scanner:
Adding two values in int a and b
package javaprogram;
import java.util.Scanner; //Scanner package
public class TopicScanner{
public static void main(String[] args) { // Scanner class object Scanner scannerobject=new Scanner(System.in);
// adding both int value a and b
// statement for a System.out.print("Enter a value of a = "); int a=scannerobject.nextInt();//input by user a value at runtime // statement for b System.out.print("Enter a value of b = "); int b=scannerobject.nextInt();//input by user a value at runtime int c=a+b;//c variable for add a and b // added value System.out.println("added value of a and b is "+c);
} }
Example program for Scanner class all methods
package javaprogram;
import java.util.Scanner; // Scanner package
public class TopicScanner{
public static void main(String[] args) {
//Scanner class object
Scanner scannerobject=new Scanner(System.in);
//numbers byte we pass input -128 to 127
System.out.print("Enter byte value of a = "); byte a=scannerobject.nextByte();//input by user a value at runtime
//numbers short we pass input -32,768 to 32,767
System.out.print("Enter short value of b = "); short b=scannerobject.nextShort();//input by user a value at runtime //numbers int we pass input -2,147,483,648 to 2,147,483,647 System.out.print("Enter int value of c = "); int c=scannerobject.nextInt();//input by user a value at runtime
//numbers long we pass input
9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 System.out.print("Enter long value of d = "); long d=scannerobject.nextLong();//input by user a value at runtime
//decimal float we pass input Approx. 333.438 (7
decimal digits after point) System.out.print("Enter float value of e = "); float e=scannerobject.nextFloat();//input by user a value at runtime
//decimal double we pass input Approx. 11.8308 (15
decimal digits after point) System.out.print("Enter double value of f = "); double f=scannerobject.nextDouble();//input by user a value at runtime
//character char we pass input single value in
single quotes System.out.print("Enter char value of g = "); char g=scannerobject.next().charAt(1);//input by user a value at runtime
//in String we can't use both at same time
//sequence of character String we pass input characters in double quotes System.out.print("Enter String value of h = "); String h=scannerobject.next();//input by user a value at runtime //sequence of character String we pass input characters in double quotes System.out.print("Enter String value of i = "); String i=scannerobject.nextLine(); //input by user a value at runtime
//boolean used for condition we can pass input true
or false System.out.println("Enter boolean value of j = "); boolean j=scannerobject.nextBoolean();//input by user a value at runtime
//after entered value at runtime its execute all
values using this printing statements System.out.println("value of byte "+a); System.out.println("value of short "+b); System.out.println("value of int "+c); System.out.println("value of long "+d); System.out.println("value of float "+e); System.out.println("value of double "+f); System.out.println("value of char "+g); System.out.println("value of String "+h); System.out.println("value of String "+i); System.out.println("value of boolean "+j);
} }
Using a nextLine() method of String after int it
doesn't ask to pass input so need to next() method of String
2,147,483,647 System.out.print("Enter int value of a = "); int a=scannerobject.nextInt();//input by user a value at runtime
//if using nextLine() method in string after int a
string will ask input so need to use next() method //sequence of character String we pass input characters in double quotes System.out.print("Enter String value of b = "); String b=scannerobject.nextLine();//This will not work after int
String b1=scannerobject.next();//we can use this
after int
System.out.println("value of int "+a);//input pass
by user System.out.println("value of String "+b);//input pass by user } }