User Input using Scanner
Java में input पढ़ना
Program को interactive बनाने को, उसे user से input पढ़ना होगा। Java में सबसे आसान तरीका Scanner class है। आप keyboard (System.in) से जुड़ा Scanner बनाते हैं और उसकी methods से text व numbers पढ़ते हैं।
nextInt(), दशमलव को nextDouble(), text को nextLine()।Scanner import और बनाना
import java.util.Scanner; // ऊपर
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// input पढ़ने को तैयार
}
}
पहले import java.util.Scanner;, फिर Scanner sc = new Scanner(System.in); बनाइए। System.in यानी keyboard। अब sc पढ़ सकता है user क्या type करता है।
Text और numbers पढ़ना
String name = sc.nextLine(); // text की पूरी line String word = sc.next(); // एक शब्द int age = sc.nextInt(); // पूर्ण संख्या double pay = sc.nextDouble(); // दशमलव संख्या
हर method एक ख़ास type पढ़ती है। spaces वाले text को nextLine(), एक शब्द को next(), और numbers को nextInt()/nextDouble() इस्तेमाल कीजिए।
एक पूरा उदाहरण
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.nextLine();
System.out.print("Enter your age: ");
int age = sc.nextInt();
System.out.println("Hi " + name + ", age " + age);
sc.close();
}
}
Enter your age: 18
Hi Aman, age 18
हम prompt करते हैं, नाम और age पढ़ते हैं, फिर print करते हैं। अंत में sc.close() Scanner मुक्त करता है।
nextInt के बाद nextLine bug
int age = sc.nextInt(); sc.nextLine(); // FIX: बचा newline consume करो String name = sc.nextLine();
Classic जाल: nextInt() के बाद, Enter key buffer में बच जाती है, तो अगला nextLine() खाली line पढ़ लेता है। हल है number के ठीक बाद एक अतिरिक्त sc.nextLine(), उस बचे newline को निगलने को।
Scanner के साथ practice programs
ये तीन programs school और college की practical files में आम हैं। हर program javac से compile करके दिखाए गए input के साथ चलाया गया है (type की गई values हर prompt के बाद दिखती हैं)।
Program 1: तीन संख्याओं का योग और औसत
import java.util.Scanner;
public class SumAverage {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter three numbers: ");
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int sum = a + b + c;
double avg = sum / 3.0;
System.out.println("Sum = " + sum);
System.out.printf("Average = %.2f%n", avg);
sc.close();
}
}
Sum = 47
Average = 15.67
nextInt() एक line में space से अलग कई numbers पढ़ सकता है। 3.0 (3 नहीं) से भाग देने पर दशमलव वाला औसत मिलता है।
Program 2: Marksheet – नाम, 5 subjects, प्रतिशत और grade
import java.util.Scanner;
public class Marksheet {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Student name: ");
String name = sc.nextLine();
int total = 0;
for (int i = 1; i <= 5; i++) {
System.out.print("Marks in subject " + i + ": ");
total += sc.nextInt();
}
double percent = total / 5.0;
String grade = percent >= 90 ? "A1" : percent >= 75 ? "A2" : percent >= 60 ? "B1" : percent >= 33 ? "C" : "Needs improvement";
System.out.println(name + ": total " + total + "/500, " + percent + "%, grade " + grade);
sc.close();
}
}
Marks in subject 1: 88
Marks in subject 2: 92
Marks in subject 3: 79
Marks in subject 4: 95
Marks in subject 5: 84
Riya Sharma: total 438/500, 87.6%, grade A2
नाम पहले nextLine() से पढ़ा गया है, किसी nextInt() से पहले, इसलिए newline वाली गड़बड़ी नहीं होती। Ternary chain grade चुनती है।
Program 3: Simple interest calculator
import java.util.Scanner;
public class SimpleInterest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Principal (Rs): ");
double p = sc.nextDouble();
System.out.print("Rate (% per year): ");
double r = sc.nextDouble();
System.out.print("Time (years): ");
double t = sc.nextDouble();
double si = p * r * t / 100;
System.out.printf("Simple interest = Rs %.2f%n", si);
System.out.printf("Total amount = Rs %.2f%n", p + si);
sc.close();
}
}
Rate (% per year): 7.5
Time (years): 3
Simple interest = Rs 5625.00
Total amount = Rs 30625.00
nextDouble() 7.5 जैसी दशमलव values पढ़ता है; printf("%.2f") पैसे को दो दशमलव तक दिखाता है।
ख़ुद करके देखें: Program 2 में subjects की संख्या user से पढ़ें, और Program 3 में हर साल का ब्याज भी print करें।
आम गलतियाँ
- Import भूलना। ऊपर
import java.util.Scanner;ज़रूरी है। - nextLine-after-nextInt bug। ठीक करने को एक अतिरिक्त
sc.nextLine()जोड़िए। - पूरे नामों को next() इस्तेमाल करना।
next()space पर रुकता है —nextLine()इस्तेमाल कीजिए। - Type के लिए गलत method। text को
nextInt()से पढ़ना error देता है।
सारांश
- User input पढ़ने को
Scannerclass इस्तेमाल कीजिए। - इसे import कीजिए, फिर
new Scanner(System.in)बनाइए। nextInt(),nextDouble(),next(),nextLine()अलग types पढ़ते हैं।nextInt()के बाद अतिरिक्तnextLine()से newline bug ठीक कीजिए।- ख़त्म होने पर
sc.close()से Scanner close कीजिए।
आगे, input को फ़ैसलों में इस्तेमाल कीजिए if-else से, या इसे process कीजिए Java loops में।
अक्सर पूछे जाने वाले प्रश्न (FAQ)
Java में user से input कैसे लूँ?
Scanner class इस्तेमाल कीजिए। इसे import java.util.Scanner; से import कीजिए, Scanner sc = new Scanner(System.in); से बनाइए, फिर sc.nextInt() और sc.nextLine() जैसी methods से values पढ़िए।next() और nextLine() में क्या अंतर है?
next() एक शब्द पढ़ता है (space पर रुककर), जबकि nextLine() spaces समेत पूरी line पढ़ता है। जब input में spaces हो सकते हों, जैसे पूरा नाम, तब nextLine() इस्तेमाल कीजिए।Java में number कैसे पढ़ूँ?
nextInt() और दशमलव को nextDouble() इस्तेमाल कीजिए। ये number सीधे पढ़ते हैं, तो आपको खुद text से convert नहीं करना पड़ता।nextInt() के बाद nextLine() क्यों skip हो जाता है?
nextInt() input buffer में newline (Enter) छोड़ देता है, और अगला nextLine() उस बचे newline को खाली line के रूप में पढ़ लेता है। इसका हल है उसे consume करने को एक अतिरिक्त sc.nextLine() जोड़ना।क्या Scanner close करना ज़रूरी है?
sc.close() call करना अच्छी practice है, resources मुक्त करने को। छोटे programs को यह ज़रूरी नहीं, पर यह साफ़ आदत है।