Java programming scanner class usage example

When writing programs in eclipse, if our variables need to be entered manually, we can use the scanner class.

Scanner class, a new utility for scanning input text. As the first mock exam, any data must be retrieved or retrieved by using an index. Regular expressions can then be used in combination with methods to retrieve specific types of data items from the input stream. In this way, in addition to using regular expressions, the scanner class can also arbitrarily analyze strings and basic types of data, such as int and double. With the help of scanner, you can write a custom parser for any text content to be processed.

Note: in eclipse, be sure to import Java. Java under package at the beginning util. Scanner; Otherwise, the scanner cannot be called.

For example:

Output:

Enter a string and enter:

In addition to string, other types can be entered. We just need to set string M = SCN You can enter an integer by changing the string in nextline() to int, and so can others.

Tips:

Use eclipse to format java code. The shortcut key is Ctrl + Shift + F.

The above code format is deliberately disturbed:

After formatting:

Isn't it much better.

summary

The above is all about the usage examples of Java programming scanner class in this article. I hope it will be helpful to you. Interested friends can continue to refer to other related topics on this site. If there are deficiencies, please leave a message to point out. Thank you for your support!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>