JAVA basic programming exercises [7] program 7 Working with strings

 

[7] string processing program 7

Title: Enter the line of characters, respectively, the statistics of the number of letters, spaces, numbers and other characters in them.

Program Analysis: Using the while statement, the conditions for character input is not '\ n'.

 

package cskaoyan;

public class cskaoyan7 {
	@org.junit.Test
	public void count() {
		int letterNumber = 0;
		int spaceNumber = 0;
		int digitNumber = 0;
		int otherNumber = 0;

		java.util.Scanner in = new java.util.Scanner(System.in);
		String str = in.nextLine();
		char[] ch = str.toCharArray();
		int i = 0;

		while (i < ch.length) {
			if (Character.isLetter(ch[i])) {
				letterNumber++;
			} else if (Character.isWhitespace(ch[i])) {
				spaceNumber++;
			} else if (Character.isDigit(ch[i])) {
				digitNumber++;
			} else {
				otherNumber++;
			}

			++ I; 
		} 

		in.close (); 

		System.out.println ( "the number of letters:" + letterNumber); 
		System.out.println ( "number of spaces:" + spaceNumber); 
		System.out.println ( "number of digits:" + digitNumber); 
		System.out.println ( "the number of other characters:" + otherNumber); 
	} 
}

 

Guess you like

Origin www.cnblogs.com/denggelin/p/11297042.html