कोडजिम युनिव्हर्सिटी कोर्सचा एक भाग म्हणून मार्गदर्शकासह व्याख्यान स्निपेट. पूर्ण अभ्यासक्रमासाठी साइन अप करा.


"अमिगो, तुझी वेळ आली आहे. मी आता तुला कीबोर्ड इनपुटबद्दल सांगणार आहे."

"आम्ही स्क्रीनवर डेटा प्रदर्शित करण्यासाठी System.out चा वापर केला आहे. इनपुट प्राप्त करण्यासाठी, आम्ही System.in वापरू ."

"सोपं वाटतंय."

"परंतु System.in मध्ये एक कमतरता आहे - ती आम्हाला फक्त कीबोर्डवरील अक्षर कोड वाचू देते. या समस्येवर जाण्यासाठी आणि एकाच वेळी डेटाचे मोठे भाग वाचण्यासाठी, आम्ही अधिक जटिल रचना वापरू:"

उदाहरण 1
कीबोर्डवरून स्ट्रिंग आणि नंबर इनपुट करा
InputStream inputStream = System.in;
Reader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

String name = bufferedReader.readLine(); //Read a string from the keyboard
String sAge = bufferedReader.readLine(); //Read a string from the keyboard
int nAge = Integer.parseInt(sAge); //Convert the string to a number.
1
टास्क
Java Syntax,  पातळी 3धडा 7
लॉक केलेले
Code entry
Sometimes you don't need to think, you just need to hammer it out! As paradoxical as it may seem, sometimes your fingers will "remember" better than your conscious mind. That's why while training at the secret CodeGym center you will sometimes encounter tasks that require you to enter code. By entering code, you get used to the syntax and earn a little dark matter. What's more, you combat laziness.
उदाहरण 2
मागील उदाहरणाची अधिक संक्षिप्त आवृत्ती:
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

String name = reader.readLine();
String sAge = reader.readLine();
int nAge = Integer.parseInt(sAge);
उदाहरण 3
आणखी कॉम्पॅक्ट
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int age = scanner.nextInt();

"काही प्रश्न?"

"अरे...मला काहीच समजले नाही."

"कीबोर्डवरील स्ट्रिंग वाचण्यासाठी, BufferedReader ऑब्जेक्ट वापरणे सर्वात सोयीचे आहे. परंतु हे करण्यासाठी, आपण ज्या ऑब्जेक्टमधून डेटा वाचणार आहात त्यामधून तुम्हाला पास करावे लागेल. या प्रकरणात, System.in ."

"परंतु System.in आणि BufferedReader विसंगत आहेत, म्हणून आम्ही दुसरे अडॅप्टर वापरतो - दुसरा InputStreamReader ऑब्जेक्ट."

"मला वाटतं मला आता ते मिळालं. हा स्कॅनर वर्ग काय आहे ?"

"स्कॅनर सोयीस्कर असू शकते, परंतु ते फारसे उपयुक्त नाही. गोष्ट अशी आहे की, तुम्ही पुढे जाल (अभ्यास आणि काम करताना), तुम्ही बफरेडरीडर आणि इनपुटस्ट्रीमरीडरचा वापर कराल , परंतु स्कॅनर - फार क्वचितच. आमच्या उदाहरणात ते सोयीचे आहे, परंतु भविष्यात ते वारंवार उपयोगी पडणार नाही. त्यामुळे आम्ही त्याचा जास्त वापर करणार नाही ."

"ते स्पष्ट दिसते आहे, परंतु मला खात्री नाही की मला सर्वकाही समजले आहे."