"66" Scanner class usage

Source: Internet
Author: User

Scanner is a new simple text scanner that was not there before JDK 5.0.
publicfinalclass Scanner extends Object implements Iterator<String>, Closeable 

Visible, scanner is no subclass.
In the JDK API about scanner provides a much more constructive method and method. So now list some of the usual methods of work in peacetime, for your reference only:

Construction Method:
publicScannerthrowspublicScannerpublicScanner(InputStream source) //用指定的输入流来创建一个Scanner对象

Scanner reader=new Scanner (system.in);
The reader object then calls the following methods (functions) to read the various data types that the user entered at the command line:

Method:

public void Close ()//close
Public Scanner Usedelimiter (string pattern)//Set delimited mode, string can be replaced with pattern
public boolean Hasnext ()//detection input, whether, and words
Public String Next ()//Read the next word, by default, spaces as separators
Public String nextline ()//Read line
Nextbyte (), nextdouble (), Nextfloat,nextint (), nextline (), Nextlong (), Nextshort () and other methods

One, get console input
Import Java.util.Scanner; Public classTestscanner { Public Static void Main(string[] args) {Scanner SCN =NewScanner (System.inch); System. out. println ("Waiting Input"); while(true) {String temp = scn.nextline ();if(Temp.trim (). Equals ("Exit")) {return; } System. out. println ("Your input is:"+ temp); }      }  }

By creating a Scanner with new Scanner (sysem.in), the console waits for input until the end of the hit-return and passes the input to Scanner as the scanned object. If you want to get the input, you only need to call Scanner's nextline () (or Next ()) method.

Scanner can allow multiple lines to be entered
next() 每次取到一个间隔符前面的数据   如: 输入 Hello World 取值应该是Hello,因为Hello后面有空格nextLine() 每次取一个换行符前面的数据  如:输入 Hello World 回车,取值 就是Hello WorldnextInt() 是取next() 然后把字符串解析成一个int数字。(此外,还有nextLong()、nextShort()方法,可参见API)hasNextInt() 是判断下次调用next()是否可以得到一个可以安全解析成int的字符串。如果已经到达输入的结尾,或者下一个next()的返回值不能解析为一个数字,即不符合数字的格式,那么返回false。

Reference:
http://blog.csdn.net/yiyiwudian/article/details/46522415
http://blog.csdn.net/basycia/article/details/51112278
http://isunshine.blog.51cto.com/2298151/880038

My QR code is as follows, welcome to exchange discussion

You are welcome to pay attention to the "It question summary" subscription number. Every day to push the classic face test and interview tips, are dry! The QR code of the subscription number is as follows:

"66" Scanner class usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.