Java Learning diary-2 bits

Source: Internet
Author: User

First, using the Scanner class for console input

The scanner class in the document is defined as a simple text scanner which can parse primitive types and strings using regular expressions (one can use regular expressions to parse the base Simple text scanners of this type and string).

To use the Scanner class, first import the Import.util.Scanner;

Then set the object Scanner scan = new Scanner (system.in);

You can then receive a variety of data types from the console input.

Like what:

int x = Scan.nextint ();

String s = scan.next ();

String s = scan.nextline ();

(The difference between next and Nextline is that the former receives a string up to the first space, and the latter receives a string that can contain spaces, until the carriage return)

Double d = scan.nextdouble ();

If you want to enter a character, you can take advantage of the Charat (index) method of the string.

For example, char ch = scan.next (). charAt (0);

  

Java Learning diary-2 bits

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.