[Solved] java characters in a string


This should do it. What it does is that it gets a string to look at, gets a character to look at, iterates through the string looking for matches, counts the number of matches, and then returns the information. There are more elegant ways to do this (for example, using a regex matcher would also work).

@SuppressWarnings("resource") Scanner scanner = new Scanner(System.in);

System.out.print("Enter a string:\t");
String word = scanner.nextLine();

System.out.print("Enter a character:\t");
String character = scanner.nextLine();

char charVar = 0;
if (character.length() > 1) {
    System.err.println("Please input only one character.");
} else {
    charVar = character.charAt(0);
}

int count = 0;
for (char x : word.toCharArray()) {
    if (x == charVar) {
        count++;
    }
}

System.out.println("Character " + charVar + " appears " + count + (count == 1 ? " time" : " times"));

2

solved java characters in a string