JavaAlgorithms/Others/CountChar.java

25 lines
647 B
Java
Raw Normal View History

package Others;
2017-07-08 09:12:14 +08:00
import java.util.Scanner;
public class CountChar {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter your text: ");
String str = input.nextLine();
2018-01-24 23:57:30 +08:00
input.close();
2017-07-08 09:12:14 +08:00
System.out.println("There are " + CountCharacters(str) + " characters.");
}
2017-07-08 09:12:14 +08:00
/**
2019-10-29 12:40:39 +08:00
* Count non space character in string
*
* @param str String to count the characters
* @return number of character in the specified string
*/
2018-01-24 23:58:38 +08:00
private static int CountCharacters(String str) {
2019-10-29 12:40:39 +08:00
return str.replaceAll("\\s", "").length();
}
2017-07-08 09:12:14 +08:00
}