Question 03: programming question-enter a string, count the number of characters, numbers, and other types ., 03 string
I went to the test yesterday and encountered a programming question, because I often sat in front of my computer and used development tools to get used to "alt +/". As a result, I didn't pay much attention to some methods at ordinary times, so after sorting out the ideas, when writing the method, it's finished, and it's stuck .... as a result, the question is not satisfactory .... now I will sort it out, hoping to deepen my impression and help readers.
Question: enter a string on the keyboard, and calculate the number of characters, numbers, spaces, and other characters in the string.
With input, the first thing I thought of at the time was the category class. After the variable was defined, after the producer instance was created, I found that the method was forgotten and stuck here... now, paste the code that comes back below:
Package com. ygh. pencilTest; import java. util. character;/*** enter a string from the keyboard, and then calculate the characters and numbers in the string, number of spaces and other characters ** @ author night cold * @ version 1.1.1 */public class JudgeString_01 {public static void main (String [] args) {System. out. println ("enter a string:"); int digital = 0; // number int character = 0; // english int blank = 0; // space int other = 0; // other // enter zhangsan = new kernel (System. in); // obtain the input String str = san. nextLine (); // put the obtained string into a char array. char [] charArray = str. toCharArray (); // obtain the string in the array, and then determine the type and calculate the number of for (int I = 0; I <charArray. length; I ++) {if (charArray [I]> = '0' & charArray [I] <= '9') {digital ++ ;} else if (charArray [I]> = 'A' & charArray [I] <= 'Z ') | charArray [I]> = 'A' & charArray [I] <= 'Z') {character ++ ;} else if (charArray [I] = '') {blank ++ ;}else {other ++ ;}// output result System. out. println ("number of digits:" + digital); System. out. println ("number of letters:" + character); System. out. println ("number of spaces:" + blank); System. out. println ("other numbers:" + other );}}
Running result:
The second method is to use the BufferedReader class to read the input characters. Similarly, here the readLine () method must be remembered like the delimiter method, the attention method is the nextLine () method. So now I will paste the second method for reference, but I generally mention two words, the first thing I think of is the category class ....
Package com. ygh. pencilTest; import java. io. bufferedReader; import java. io. IOException; import java. io. inputStreamReader;/*** enter a string from the keyboard, and then calculate the characters and numbers in the string, number of spaces and other characters ** @ author night cold * @ version 1.1.1 */public class JudgeString_02 {public static void main (String [] args) throws IOException {System. out. println ("enter a string:"); int digital = 0; // number int character = 0; // english int blank = 0; // space int other = 0; // other // input BufferedReader br = new BufferedReader (new InputStreamReader (System. in); // String str = br. readLine (); // System. out. println (str); // judge for (int I = 0; I <str. length (); I ++) {// converts str to char c = str. charAt (I); // judge the type of c if (c> = '0' & (int) c <= '9') {digital ++ ;} else if (c> = 'A' & c <= 'Z') | (c> = 'A' & c <= 'Z ')) {character ++;} else if (c = '') {blank ++;} else {other ++;} // number of output systems. out. println (digital); System. out. println (blank );}}
Running result: