[英]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: 你递增i
两次,每次迭代中,所以你跳过半个汉字:
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++;
你当前的问题是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
. 它给出了"x"
出现在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 i ++ for for循环将i值递增1,无需在eloop内递增
check Working of For loop here 在这里检查For循环的工作
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.