Day 12 : Scanner Class in Java
POOJAASHREE P V
Full Stack Developer | JavaScript | Bootstrap | React | Spring Boot | MySQL
Scanner Class :
Input Type Methods :
Example :
// Read a Line of Text Using Scanner
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter your name : ");
String name = s.nextLine();
System.out.println("My name is " + name);
s.close();
}
}
Output :
Enter your name : Poojaa
My name is Poojaa
In this example :
Example :
// Java Scanner nextInt()
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter an integer : ");
int value = s.nextInt();
System.out.println("The entered integer value is " + value);
s.close();
}
}
Output :
Enter an integer : 10
The entered integer value is 10
In this example,
Example :
// Java Scanner nextDouble()
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter Double value : ");
double value = s.nextDouble();
System.out.println("The entered double value is " + value);
s.close();
}
}
Output :
Enter Double value : 11.11
The entered double value is 11.11
In this example,
Example :
// Java Scanner nextLine()
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter your name : ");
String value = s.nextLine();
System.out.println("My name is " + value);
s.close();
}
}
Output :
Enter your name : Poojaa
My name is Poojaa
In this example :
Example :
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter your name : ");
String name = s.nextLine();
System.out.print("Enter your age : ");
int age = s.nextInt();
System.out.println("Name : " + name);
System.out.println("Age : " + age);
}
}
Output :
Enter your name : Poojaa
Enter your age : 22
Name : Poojaa
Age : 22
In this example :