JavaAlgorithms/Others/CountChar.java

25 lines
603 B
Java
Raw Permalink Normal View History

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