Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Scanner in Java

2025-02-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "how to use Scanner in Java". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn how to use Scanner in Java.

Scanner

Basic grammar

Scanner s = new Scanner (System.in)

The input string is obtained by the next () and nextLine () methods of the Scanner class, and hasNext () and hasNextLine () are generally used to determine whether there is any input data before reading.

Use the next method:

Scanner scan = new Scanner (System.in)

/ / receive data from the keyboard

/ / receive string in next mode

System.out.println ("receive by next:")

/ / determine whether there is any input

If (scan.hasNext ()) {

String str1 = scan.next ()

System.out.println ("input data is:" + str1)

}

Scan.close ()

Next () method:

You must read valid characters before you can end the input.

The next () method automatically removes whitespace encountered before entering valid characters.

Use the white space entered after a valid character as a delimiter or Terminator only after entering a valid character.

Next () cannot get a string with spaces.

Use the nextLine method:

Scanner scan = new Scanner (System.in)

/ / receive data from the keyboard

/ / receive string in nextLine mode

System.out.println ("receive by nextLine:")

/ / to determine whether the http://m.zykdbh.com/ of Zhengzhou Hospital for treatment of birthmarks is still being imported.

If (scan.hasNextLine ()) {

String str2 = scan.nextLine ()

System.out.println ("input data is:" + str2)

}

Scan.close ()

NextLine () method:

With Enter as the Terminator, that is, the nextLine () method returns all the characters before entering carriage return.

You can get blanks.

If you want to enter data of type int or float, it is also supported in the Scanner class, but it is best to use the hasNextXxx () method to validate before entering, and then use nextXxx () to read it.

SA copyright Agreement, reproduced, please attach the original source link and this statement.

At this point, I believe you have a deeper understanding of "how to use Scanner in Java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report