A:
import java.util.Scanner;
public class InputCounting {
- public static int countLetters(String is){
- byte b[] = is.getBytes();
int cnt = 0;
for(int i=0; i < b.length; i++){
- if(Character.isLetter(b[i])) cnt++;
return cnt;
public static int countNumbers(String is){
- byte b[] = is.getBytes();
int cnt = 0;
for(int i=0; i < b.length; i++){
- if(Character.isDigit(b[i])) cnt++;
return cnt;
public static int countSpaces(String is){
- byte b[] = is.getBytes();
int cnt = 0;
for(int i=0; i < b.length; i++){
- if(Character.isSpaceChar(b[i])) cnt++;
return cnt;
public static int countOthers(String is){
- byte b[] = is.getBytes();
int cnt = 0;
for(int i=0; i < b.length; i++){
- if(!(Character.isSpaceChar(b[i]) ||
- Character.isDigit(b[i]) ||
Character.isLetter(b[i])))
cnt++;
- Character.isDigit(b[i]) ||
return cnt; - if(!(Character.isSpaceChar(b[i]) ||
public static void main(String[] args) {
- Scanner s = new Scanner(System.in);
String input;
while(true){
- System.out.print("Please input the counted String: ");
input = s.nextLine();
if(input.equalsIgnoreCase("Quit")) break;
System.out.println("Letter quantity: " + InputCounting.countLetters(input));
System.out.println("Number quantity: " + InputCounting.countNumbers(input));
System.out.println("Space quantity: " + InputCounting.countSpaces(input));
System.out.println("Others quantity: " + InputCounting.countOthers(input));
- System.out.print("Please input the counted String: ");
- byte b[] = is.getBytes();
result:
Please input the counted String: What are the counters for "123456"?
Letter quantity: 21
Number quantity: 6
Space quantity: 5
Others quantity: 3
Please input the counted String: Quit
No comments:
Post a Comment