Java Common Object-scanner class

Source: Internet
Author: User

2017-11-02 16:33:11

Scanner class : A simple text scanner that can use regular expressions to parse basic types and strings.

ScannerBreaks its input into tokens using the delimiter mode, which, by default, matches whitespace. You can then use a different next method to convert the resulting token to a different type of value.

* Construction Method

* Common Methods

Basic Format: hasnextxxx (): Determine if there is a next entry, where Xxx can be int,double and so on. If you need to decide whether to include the next string , you can omit Xxx.

nextxxx (): Gets the next entry.

By default , scanner uses spaces and carriage returns as delimiters.

Commonly used is the public int Nextint (), the public String nextline ().

There is a problem getting the numeric type first, and then getting the string type. This problem has also been encountered in C + +.

public static void Main (string[] args) {        Scanner sc = new Scanner (system.in);                Y will be empty        int x=sc.nextint ();        String y=sc.nextline ();        System.out.println (x+y);    }

Workaround:

A: Redefine a scanner

B: Use String to get all first, then convert

public static void Main (string[] args) {        Scanner sc = new Scanner (system.in);        Y will be empty        int x=sc.nextint ();        Scanner SC2 = new Scanner (system.in);        String y=sc2.nextline ();        System.out.println (x+y);    }

Java Common Object-scanner class

Related Article

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.