← Back to Core Java Course | Chapter 2: Input & Output | Lesson 3 of 6

Java की Scanner Class

Scanner आपके program को वह पढ़ने देता है जो एक person type करता है, एक teacher की तरह जो सवाल पूछता है और आपका answer लिखता है। आप words, numbers, और ज़्यादा पढ़ सकते हैं।
Syntax
java
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?

java
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());
	}
}

Reading Strings

next() अगले whitespace तक input पढ़ता है, इसलिए यह 'John Smith' जैसे नाम में पहले space पर रुकता है; nextLine() इसके बजाय newline तक सब कुछ पढ़ता है, spaces सहित पूरी line capture करते हुए।

उदाहरण: Reading Strings

java
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"
	}
}

Reading Numbers

nextInt() और nextDouble() जैसे methods अगले token को उस specific numeric type के रूप में parse करते हैं और अगर type किया गया असल में एक number जैसा न दिखे तो एक InputMismatchException throw करते हैं।

उदाहरण: Reading Numbers

java
// 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);
	}
}

Checking for Inputs

nextInt() से पहले hasNextInt() call करना आपको पहले यह जांचने देता है कि अगला token असल में एक valid integer है या नहीं, जो bad data पर crash हुए बिना user input validate करने का standard तरीका है।

उदाहरण: Checking for Inputs

java
// 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());
		}
	}
}

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

java
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
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. nextInt() से एक number पढ़ना और फिर nextLine(), जो एक empty string return करता है क्योंकि newline अब भी buffer में है।
  2. nextInt() call करना जब user text type करता है, जो InputMismatchException throw करता है।
  3. कोई input न बचने पर next() call करना, जो NoSuchElementException throw करता है; hasNext() जांचें।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.