Java Learning (6): Enter a string that counts the number of Chinese and English letters, spaces, numbers, and other characters, respectively.

Source: Internet
Author: User

1 ImportJava.util.Scanner;2 3 /**4 * Enter a line of characters to count the number of Chinese and English letters, spaces, numbers, and other characters, respectively .5  *6  * @authorXCX7 * @time July 22, 2017 morning 9:08:168  */9  Public classPractice7 {Ten  One      Public Static voidMain (string[] args) { ASystem.out.println ("Please enter a string, we will count the number of Chinese and English letters, spaces, numbers and other characters"); -Scanner Scanner =NewScanner (system.in); -String string =scanner.nextline (); the  -         intNumbersofblack =Getnumbersofblack (string); -         intNumbersofchinese =Getnumbersofchinese (string); -         intNumbersofothers =getnumbersofothers (string); +         intNumbersoffigure =getnumbersoffigure (string); -         intNumbersofenglish =getnumbersofenglish (string); +          ASystem.out.println ("Number:" + numbersoffigure + "X")); atSystem.out.println ("English:" + numbersofenglish + "one")); -SYSTEM.OUT.PRINTLN ("spaces are:" + numbersofblack + "X")); -System.out.println ("Kanji:" + Numbersofchinese + "one")); -System.out.println ("Others are:" + numbersofothers + "X")); -     } -  in     //to find the number of hollow lattice of a string -      Public Static intGetnumbersofblack (String string) { to         //record the number of empty cells in a string +         intNumbersofblack = 0; -         //Converts the input string into a character array theString[] strings = String.Split (""); *          for(inti = 0; i < strings.length; i++) { $             if(Strings[i].matches ("")) {Panax Notoginsengnumbersofblack++; -             } the         } +         returnNumbersofblack; A     } the  +     //to find the number of Chinese characters in a string -      Public Static intGetnumbersofchinese (String string) { $         //record the number of Chinese characters in a string $         intNumbersofchinese = 0; -         //Converts the input string into a character array -String[] strings = String.Split (""); the          for(inti = 0; i < strings.length; i++) { -             if(Strings[i].matches ("[\u4e00-\u9fa5]")) {Wuyinumbersofchinese++; the             } -         } Wu         returnNumbersofchinese; -     } About  $     //find the number of English letters in a string -      Public Static intgetnumbersofenglish (String string) { -         //the number of English letters in the record string -         intNumbersofenglish = 0; A         //Converts the input string into a character array +String[] strings = String.Split (""); the          for(inti = 0; i < strings.length; i++) { -             if(Strings[i].matches ("[A-za-z]")) { $numbersofenglish++; the             } the         } the         returnNumbersofenglish; the     } -  in     //Number of numbers in a string the      Public Static intgetnumbersoffigure (String string) { the         //Number of digits in the record string About         intNumbersoffigure = 0; the         //Converts the input string into a character array theString[] strings = String.Split (""); the          for(inti = 0; i < strings.length; i++) { +             if(Strings[i].matches ("[0-9]")) { -numbersoffigure++; the             }Bayi         } the         returnnumbersoffigure; the     } -  -     //Number of numbers in a string the      Public Static intgetnumbersofothers (String string) { the         //Number of digits in the record string the         intNumbersofothers = String.Split (""). Length the-Getnumbersofblack (String)-Getnumbersofchinese (String) --Getnumbersofenglish (String)-getnumbersoffigure (string); the         returnnumbersofothers; the     } the}

Java Learning (6): Enter a string that counts the number of Chinese and English letters, spaces, numbers, and other characters, respectively.

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.