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
<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?
onclick="window.location.href='/BASE/jsp/demo.jsp?checkFlavor=itsp&issueID=61&intro=general&group=aitu&ur=f'">
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