Register   Login   About   Study   Enterprise   Share
Internet / AI Technology University (ITU/AITU)
Fast Login - available after registration







|

Top Links: >> 80. Technology >> Internet Technology Summit Program >> 1. Java Introduction >> 1.4. Input-Output and Exceptions
Current Topic: 1.4.3. Input from Keyboard with Scanner
You have a privilege to create a quiz (QnA) related to this subject and obtain creativity score...
Besides reading files, we much more often use the keyboard as an input device.
There is a very convenient utility class java.util.Scanner, which was created to simplify computer interaction with the keyboard in Java.
This class can parse primitive types and strings using regular expressions entered via the keyboard.
Although the Scanner can work with other input streams, besides the keyboard, developers mostly use it when need to get a brief answer from a user via the keyboard.
For example, the code below can read a text line entered by a user:

// instantiate a Scanner object based on the System.in (keyboard) stream
Scanner sc = new Scanner(System.in);
// Ask a user to enter data
System.out.println("Enter your name, please");
// a user must type information and press the ENTER key
// get this data
String name = sc.nextLine();
/*
The Scanner class has several methods reading primitive data, such as nextInt(), nextLong(), etc.
Our recommendation is to always use the nextLine() method, even if you expect the number. Why? Because a user can mistakenly press not a numeric key, which will crash the nextInt() or nextLong(), etc. methods.
Here is the recommended example:
*/
// Ask a user for a number
System.out.println("Type a number and press the ENTER key");
// get the number as a String
String stringNumber = sc.nextLine();
// parse the string with the try/catch to prevent the crash
try {
int number = Integer.parseInt(stringNumber);
} catch(Exception e) {
System.out.println("Not a number! You entered: " +stringNumber);
}
Was it clear so far?

P.S. It is a good idea to close scanner input flow at the end.
How to do this?

Just type in Eclipse sc. and you will see several methods that can be used with the Scanner. One of them is close().

Assignments:

Create the TestScanner class in the same package and in the main() method as a user to enter several pieces of information: Name, Address, etc.

Use a Scanner object to read the data. Then display the data entered by user. Watch the Eclipse Console when Run As Java Application.
Email the class to dean@ituniversity.us
| Check Your Progress | Propose QnA | Have a question or comments for open discussion?
<br/>// instantiate a Scanner object based on the System.in (keyboard) stream
<br/>Scanner sc = new Scanner(System.in);
<br/>// Ask a user to enter data
<br/>System.out.println("Enter your name, please");
<br/>// a user must type information and press the ENTER key
<br/>// get this data
<br/>String name = sc.nextLine();
<br/>/*
<br/>The Scanner class has several methods reading primitive data, such as nextInt(), nextLong(), etc.
<br/>Our recommendation is to always use the nextLine() method, even if you expect the number. Why? Because a user can mistakenly press not a numeric key, which will crash the nextInt() or nextLong(), etc. methods.
<br/>Here is the recommended example:
<br/>*/
<br/>// Ask a user for a number
<br/>System.out.println("Type a number and press the ENTER key");
<br/>// get the number as a String
<br/>String stringNumber = sc.nextLine();
<br/>// parse the string with the try/catch to prevent the crash
<br/>try {
<br/>   int number = Integer.parseInt(stringNumber);
<br/>} catch(Exception e) {
<br/>   System.out.println("Not a number! You entered: " +stringNumber);
<br/>}
<br/>






Was it clear so far?


P.S. It is a good idea to close scanner input flow at the end.
How to do this?

Just type in Eclipse sc. and you will see several methods that can be used with the Scanner. One of them is close().

Assignments:

Create the TestScanner class in the same package and in the main() method as a user to enter several pieces of information: Name, Address, etc.

Use a Scanner object to read the data. Then display the data entered by user. Watch the Eclipse Console when Run As Java Application.
Email the class to dean@ituniversity.us

| Check Your Progress | Propose QnA | Have a question or comments for open discussion?

Have a suggestion? - shoot an email
Looking for something special? - Talk to me
Read: IT of the future: AI and Semantic Cloud Architecture | Fixing Education
Do you want to move from theory to practice and become a magician? Learn and work with us at Internet Technology University (ITU) - JavaSchool.com.

Technology that we offer and How this works: English | Spanish | Russian | French

Internet Technology University | JavaSchool.com | Copyrights © Since 1997 | All Rights Reserved
Patents: US10956676, US7032006, US7774751, US7966093, US8051026, US8863234
Including conversational semantic decision support systems (CSDS) and bringing us closer to The message from 2040
Privacy Policy