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:
Character
Class 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