Java Loop Exercise: Enter a string that counts how many digits, how many letters, and how many other characters the string contains.

Source: Internet
Author: User

Package practicego;import java.util.scanner;/* * 3. Enter a string that counts how many digits, how many letters, and how many other characters are contained in the string.  */public class cto {public static void main (String[] args)  {         int englishCount = 0;//  English alphabet number          int spaceCount = 0;//  Number of spaces          int numCount = 0;//  number          int otherCount = 0;//  number of other characters         scanner  sc = new scanner (system.in);         System.out.println ("Please enter a line of characters:");        string str =  Sc.nextline ();//  get console input one line of characters         char[] ch =  Str.tochararray ();//  The string you have taken into aA char array         for  (int i = 0; i <  ch.length; i++)  {            if   (Character.isletter (ch[i))  {                 //  determine if it is a letter                  englishCount++;             } else if  (Character.isspacechar (ch[i))  {                 //  determine if it is a space                  spaceCount++;             } else if  (Character.isdigit (ch[i))  {                 //  Judging whether it is digital                  numCount++;             } else {                 //  above are not considered to be other characters                  otherCount++;             }        }         system.out.println ("Number of Letters:"  + englishcount);         system.out.println ("Number of Numbers:"  + numcount);         System.out.println ("Number of Spaces:"  + spacecount);         System.out.println ("Number of other characters:"  + othercount);     }} 

Test:

Please enter a line of characters: [email protected] Number of letters: 9 Number of Numbers: 8 Number of spaces: 0 Number of other characters: 2
Please enter a line of characters: YEQC @@@123456aaa number of letters: 7 Number of digits: 6 Number of spaces: 2 Number of other characters: 3

Note: There are three classes in Java that are responsible for manipulating characters: Character, String, StringBuffer. Where the character class operates on a single character, string is an operation on a sequence of characters, and StringBuffer is an operation on a string of characters.

Statement of the 1.Character

Public final class Character extends Object implements Serializable,comparable<character>

How to construct 2.Character

Public Character (char) {}

3. Common methods

ToString () method

4. Normally, when we use characters, we sometimes need to use the characters of the original data type

char ch = ' a '; char Unichar = ' \u039a '; char[] Chararray = {' A ', ' B ', ' C ', ' d ', ' e '};

The 5.Character class provides some useful classes (that is, static) methods to manipulate characters. You can create a character for a character-constructed object

Character ch1 = new Character (' a ');


Java Loop Exercise: Enter a string that counts how many digits, how many letters, and how many other characters the string contains.

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.