Java daily Exercise (vii) count the number of letters, spaces, numbers, and other characters in a string

Source: Internet
Author: User

Title: Enter a line of characters, respectively, the number of English letters, spaces, numbers and other characters.

The string is converted into an array, and then each char is judged by several methods in the character.

The following is an introduction to the character class:

CharacterClass wraps a value of a base type in an object char . An Character object of type contains char a single field of type.

In addition, the class provides several methods to determine the categories of characters (lowercase letters, numbers, and so on), and to convert characters from uppercase to lowercase, and vice versa.

 PackageEveryday;ImportJava.util.Scanner; Public classchardistinction { Public Static voidMain (string[] args) {intAbccounter = 0; intSpacecounter = 0; intNumcounter = 0; intOthercounter = 0; Scanner Scan=NewScanner (system.in); String Str=Scan.nextline (); Char[] ch =Str.tochararray ();  for(inti = 0; i < ch.length; i++) {            if(Character.isletter (Ch[i])) {Abccounter++; }Else if(Character.isdigit (Ch[i])) {Numcounter++; }Else if(Character.isspacechar (Ch[i])) {Spacecounter++; }Else{Othercounter++; }} System.out.println ("Abccounter:" +abccounter); System.out.println ("Spacecounter:" +spacecounter); System.out.println ("Numcounter:" +numcounter); System.out.println ("Othercounter:" +othercounter); }}

Java daily Exercise (vii) count the number of letters, spaces, numbers, and other characters in a string

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.