Java की Scanner Class
In this page:
import java.util.Scanner;
Scanner scanner = new Scanner(System.in);
String text = scanner.nextLine();
int number = scanner.nextInt();
What is Scanner?
java.util से Scanner एक input source wrap करता है -- आमतौर पर keyboard input के लिए System.in, लेकिन यह उतनी ही आसानी से एक file से पढ़ सकता है -- और आपको words, lines, या numbers निकालने के लिए convenient methods देता है।
उदाहरण: What is Scanner?
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner("25 apples"); // wraps a source; System.in is typical, this is a hardcoded stand-in
System.out.println(sc.nextInt() + " " + sc.next());
}
}
Login to try C/C++/Java/PHP code in the editor
Reading Strings
next() अगले whitespace तक input पढ़ता है, इसलिए यह 'John Smith' जैसे नाम में पहले space पर रुकता है; nextLine() इसके बजाय newline तक सब कुछ पढ़ता है, spaces सहित पूरी line capture करते हुए।
उदाहरण: Reading Strings
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner("John Smith");
System.out.println(sc.next()); // stops at the first space: "John"
Scanner sc2 = new Scanner("John Smith");
System.out.println(sc2.nextLine()); // reads the whole line: "John Smith"
}
}
Login to try C/C++/Java/PHP code in the editor
Reading Numbers
nextInt() और nextDouble() जैसे methods अगले token को उस specific numeric type के रूप में parse करते हैं और अगर type किया गया असल में एक number जैसा न दिखे तो एक InputMismatchException throw करते हैं।
उदाहरण: Reading Numbers
// Import java.util.Scanner so it can be used by its short name
import java.util.Scanner;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new Scanner object and store it in sc
Scanner sc = new Scanner("42 3.14");
// Declare whole and set it to sc.nextInt()
int whole = sc.nextInt();
// Declare decimal and set it to sc.nextDouble()
double decimal = sc.nextDouble();
// Print a line to the console
System.out.println(whole + " " + decimal);
}
}
Login to try C/C++/Java/PHP code in the editor
Checking for Inputs
nextInt() से पहले hasNextInt() call करना आपको पहले यह जांचने देता है कि अगला token असल में एक valid integer है या नहीं, जो bad data पर crash हुए बिना user input validate करने का standard तरीका है।
उदाहरण: Checking for Inputs
// Import java.util.Scanner so it can be used by its short name
import java.util.Scanner;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new Scanner object and store it in sc
Scanner sc = new Scanner("42");
// Check whether sc.hasNextInt()
if (sc.hasNextInt()) {
// Print a line to the console
System.out.println("Valid int: " + sc.nextInt());
}
}
}
Login to try C/C++/Java/PHP code in the editor
Closing the Scanner
Scanner अपने input source से एक open connection रखता है, इसलिए खत्म होने पर close() call करना उस underlying resource को free करता है -- इसे skip करना console input के लिए शायद ही कभी visible problems का कारण बनता है, लेकिन यह file-backed Scanners के लिए एक resource leak है।
उदाहरण: Closing the Scanner
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner("done");
System.out.println(sc.next());
sc.close(); // frees the underlying resource
}
}
Login to try C/C++/Java/PHP code in the editor
nextInt()से एक number पढ़ना और फिरnextLine(), जो एक empty string return करता है क्योंकि newline अब भी buffer में है।nextInt()call करना जब user text type करता है, जोInputMismatchExceptionthrow करता है।- कोई input न बचने पर
next()call करना, जोNoSuchElementExceptionthrow करता है;hasNext()जांचें।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: