简体   繁体   中英

Counting character occurrences in a string - Java

I'm having trouble writing a method that takes a string and counts the number of times the first character (or really any character) appears. The code I've written is below, but for some reason keeps returning half the correct count when I run it. Any help would be appreciated, thanks.

public static void countOccurrences(String string1) {
    int counter = 0;
    char toCheck = string1.charAt(0);
    char compareChar;
    for (int i = 0; i < string1.length(); i++) {
        compareChar = string1.charAt(i);
        if (toCheck == compareChar) {
            counter++;
        }
        i++;
    }
    System.out.println(toCheck + " appears " + counter + " times in string1.");
}

You're incrementing i twice in each iteration, so you are skipping half the characters:

for (int i = 0; i < string1.length(); i++) { // i is incremented here

  compareChar = string1.charAt(i);
  if (toCheck == compareChar){
      counter++ ;
  }
  i++ ; // i is incremented again here - remove this line

}

Your immediate problem is that i++; happens twice per iteration.

An alternative approach is to use

s.length() - s.replace("x", "").length()

which gives you the number of times "x" appears in s . Arguably it's not the most efficient way, but it's clear.

i++ in for loop is incrementing the i value by 1, no need to increment inside th eloop

check Working of For loop here

  public static void countOccurrences(String string1) {

  int counter = 0;

  char toCheck = string1.charAt(0);

  char compareChar;

  for (int i = 0; i < string1.length(); i++) { 

      compareChar = string1.charAt(i);
      if (toCheck == compareChar){
          counter++ ;
      }
  }
  System.out.println(toCheck + " appears " + counter + " times in string1.");
}
    /* Most common string occurrence related solutions using java 8 */
    
    //find all character occurrences in a string
    String myString = "test";
    List<Character> list = myString.chars().mapToObj(c -> (char)c).collect(Collectors.toList());
    list.stream().distinct().forEach(c -> System.out.println(c + " = " + Collections.frequency(list, c)));

    //find one specific character occurrence in a string
    String myString = "test";
    char search = 't';
    long count = myString.chars().filter(c -> c == search).count();
    System.out.println(count);

    //find all unique characters in a string
    String myString = "test";
    List<Character> list = myString.chars().mapToObj(c -> (char)c).collect(Collectors.toList());
    list.stream().filter(c -> Collections.frequency(list,c) == 1).forEach(System.out::println);

    //find first unique character in a string
    String myString = "test";
    List<Character> list = myString.chars().mapToObj(c -> (char)c).collect(Collectors.toList());
    char firstUniqueChar = list.stream().filter(c -> Collections.frequency(list,c) == 1).findFirst().get();
    System.out.println(firstUniqueChar);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM