Register Login
Internet / AI Technology University (ITU/AITU)





|

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? Highlight the text in question Or

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
Questions and Answers (QnA): QnA1 | QnA2
We offer a fun way to share your experience and be rewarded.
You create a puzzle - quiz with a good question and several answers, one correct and several wrong ones.
The question as well as answers can include text, links, video.
This is your creation, completely up to your taste, humor, and of course your knowledge.
If there is an existing quiz above, you need first to solve the puzzles and evaluate the quality (at the bottom of every quiz).
After finishing the existing quiz you will be able to create your own. The best puzzles will be rewarded!
We invite you to create your own questions and answers (QnA) to increase your rank and win the Top Creativity Prize!

Topic Graph | 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? Highlight the text in question

Or


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
Questions and Answers (QnA): QnA1 | QnA2

We offer a fun way to share your experience and be rewarded.
You create a puzzle - quiz with a good question and several answers, one correct and several wrong ones.
The question as well as answers can include text, links, video.
This is your creation, completely up to your taste, humor, and of course your knowledge.
If there is an existing quiz above, you need first to solve the puzzles and evaluate the quality (at the bottom of every quiz).
After finishing the existing quiz you will be able to create your own. The best puzzles will be rewarded!

We invite you to create your own questions and answers (QnA) to increase your rank and win the Top Creativity Prize!


Topic Graph | 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 AI
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