Scanner is a class in java.util package used for obtaining the input of the primitive types like int, double, etc. and strings.
It is the easiest way to read input in a Java program, though not very efficient if you want an input method for scenarios where time is a constraint like in competitive programming.- To create an object of Scanner class, we usually pass the predefined object System.in, which represents the standard input stream. We may pass an object of class File if we want to read input from a file.
- To read numerical values of a certain data type XYZ, the function to use is nextXYZ(). For example, to read a value of type short, we can use nextShort()
- To read strings, we use nextLine().
- To read a single character, we use next().charAt(0). next() function returns the next token/word in the input as a string and charAt(0) function returns the first character in that string.
Example I: Scanner userinput
package com.cerotid.fundamentals;
import java.util.Scanner;
public class ScannerInputConcept2 {
public static void main(String[] args) {
int i = 1;
do {
System.out.println("Enter your name");
Scanner input = new Scanner(System.in);
String userName = input.nextLine();
printUserInput(userName);
i++;
}while(i<=5);
}
private static void printUserInput(String userName) {
// TODO Auto-generated method stub
System.out.println("You entered: "+userName);
}
}
Output:
Enter your name
Ram
You entered: Ram
Enter your name
Shyam
You entered: Shyam
Enter your name
hari
You entered: hari
Enter your name
Ramesh
You entered: Ramesh
Enter your name
Kushal
You entered: Kushal
Example II:
package com.cerotid.fundamentals;
import java.util.Scanner;
public class ScannerInputConcept2 {
public static void main(String[] args) {
int i = 1;
do {
System.out.println("Enter your name");
Scanner input = new Scanner(System.in);
String userName = input.nextLine();
printUserInput(userName);
int age = input.nextInt();
printUserInput(age);
i++;
}while(i<=5);
}
private static void printUserInput(int age) {
// TODO Auto-generated method stub
System.out.println("You entered: "+age);
}
private static void printUserInput(String userName) {
// TODO Auto-generated method stub
System.out.println("You entered: "+userName);
}
}
Output:
Enter your name
Ram
You entered: Ram
23
You entered: 23
Enter your name
Hari
You entered: Himal
45
You entered: 45
Enter your name
Hari
You entered: Hari
34
You entered: 34
Enter your name
Naresh
You entered: Naresh
35
You entered: 35
Enter your name
Ritesh
You entered: Ritesh
30
You entered: 30
Example III: when you define object instead of String then you can write both userName and age.
package com.cerotid.fundamentals;
import java.util.Scanner;
public class ScannerInputConcept2 {
public static void main(String[] args) {
int i = 1;
do {
System.out.println("Enter your name");
Scanner input = new Scanner(System.in);
String userName = input.nextLine();
printUserInput(userName);
int age = input.nextInt();
printUserInput(age);
i++;
}while(i<=5);
}
/*private static void printUserInput(int age) {
// TODO Auto-generated method stub
System.out.println("You entered: "+age);
}*/
private static void printUserInput(Object userName) {
// TODO Auto-generated method stub
System.out.println("You entered: "+userName);
}
}
Output:
Enter your name
Ram
You entered: Ram
33
You entered: 33
Enter your name
Hari
You entered: Hari
23
You entered: 23
Enter your name
Shyam
You entered: Shyam
13
You entered: 13
Enter your name
Mahesh
You entered: Mahesh
12
You entered: 12
Enter your name
Rupesh
You entered: Rupesh
10
You entered: 10
Example IV:
package com.cerotid.fundamentals;
import java.util.Scanner;
public class ScannerInputConcept2 {
public static void main(String[] args) {
int i = 1;
do {
System.out.println("Enter your name");
Scanner input = getScannerInput();
String userName = input.nextLine();
printUserInput(userName);
int age = input.nextInt();
printUserInput(age);
i++;
}while(i<=5);
}
private static Scanner getScannerInput() {
// TODO Auto-generated method stub
return new Scanner(System.in);
}
private static void printUserInput(Object userName) {
// TODO Auto-generated method stub
System.out.println("You entered: "+userName);
}
}
Output:
Enter your name
Ram
You entered: Ram
33
You entered: 33
Enter your name
Hari
You entered: Hari
23
You entered: 23
Enter your name
Shyam
You entered: Shyam
13
You entered: 13
Enter your name
Mahesh
You entered: Mahesh
12
You entered: 12
Enter your name
Rupesh
You entered: Rupesh
10
You entered: 10
package com.cerotid.fundamentals;
import java.util.Scanner;
public class ScannerInputConcept2 {
static Scanner input;
public static void main(String[] args) {
int i = 1;
getScannerInput();
do {
System.out.println("Enter your name");
String userName = input.nextLine();
printUserInput(userName);
int age = input.nextInt();
printUserInput(age);
i++;
} while(i<=5);
}
private static void getScannerInput() {
if(input == null) {
input = new Scanner(System.in);
}
}
private static void printUserInput(Object userName) {
// TODO Auto-generated method stub
System.out.println("You entered: "+userName);
}
}
No comments:
Post a Comment