The use of the scanner class in Java

Source: Internet
Author: User
Tags scanner class in java

When writing a program in Eclipse, we can use the scanner class if our variables are required to be entered manually.

The scanner class, which is a new utility for scanning input text. Because any data must be retrieved through a capturing group of the same pattern or by using a draw to retrieve parts of the text. You can then use a combination of regular expressions and methods to retrieve specific types of data items from the input stream. In this way, in addition to the ability to use regular expressions, the scanner class can optionally parse data for strings and basic types such as int and double. With scanner, you can write a custom parser for any text content you want to work with.

Note: Be sure to import java.util.Scanner under the package at the beginning of eclipse, otherwise Scanner cannot be called.

Gca

Package com.lovo.homework;
Enter a, then sequentially save A to Z data into an array, and traverse the
Print A to Z lowercase characters.
Import Java.util.Scanner;
public class Homework2016_11_17_1 {
public static void Main (string[] args) {
Scanner scn=new Scanner (system.in);
System.out.println ("Please enter string A:");
String M=scn.nextline ();//input string
String y=m.tolowercase ();
Char Chr=y.tochararray () [0];//converts a string to a character
for (int i = 0; i <; i++) {
System.out.println ("chr[" +i+ "]=" +CHR);
chr++;
}
}
}

In addition to the string, the other types can be entered, we just need to change the string m=scn.nextline () to int can enter an integer, the other is the same.

The use of the scanner class in Java

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.