计算字符串中的唯一字符

时间:2015-09-02 13:15:05

标签: java character unique counting

所以我一直在尝试制作一个代码,用于计算字符串中单词的数量,这非常简单。当我试图让它计算字符串中唯一字符的数量时,我遇到了问题。程序编译并运行它不会显示唯一字符的数量。添加System.out.println(countOfUniqueChars);以下回报不起作用。

以下是代码:

public class Uniquechar{
public static void main(String[] args) {

    String s = "Jag vet inte vad jag heter idag";
    String[] parts = s.split(" ");
    int wordcount = parts.length;
    System.out.println("The number of words is" + wordcount);

    countUniqueCharacters(s);
}

public static int countUniqueCharacters(String s) {
    String lowerCase = s.toLowerCase();
    char characters[] = lowerCase.toCharArray();
    int countOfUniqueChars = s.length();
    for (int i = 0; i < characters.length; i++) {
        if (i != lowerCase.indexOf(characters[i])) {
            countOfUniqueChars--;
        }
    }
    return countOfUniqueChars;
}

8 个答案:

答案 0 :(得分:2)

试试这个:

s = s.replace(" ", ""); // If you don't want to count space
char[] chars = s.toCharArray();
Set<Character> uniqueChars = new HashSet<>();

for (char c : chars) {
   uniqueChars.add(c);
}

System.out.println(c.size());

答案 1 :(得分:1)

只需打印方法调用,即可打印结果。

public EodActivityEntities()
    : base("name=DatabaseNameEntities")
{
}
  

添加System.out.println(countOfUniqueChars);以下回报不起作用。

它不会起作用。因为返回语句后的代码无法访问。也许你可以在 System.out.println(countUniqueCharacters(s)); 之前完成。

return

答案 2 :(得分:0)

您可以在main方法中执行System.out.println(countUniqueCharacters(s));,以输出方法的返回值。返回后,您无法添加更多代码。我为你做了,输出是12,所以你的算法似乎也有问题。

    int uniqeCharsCount = countUniqueCharacters(s);
    System.out.println("The number of uniqe chars is " + uniqeCharsCount);

输出:12

您的算法:

实际上,您正在检查每个字符,如果此字符串再次出现在字符串before中。但是你还应该检查char是否在当前索引的字符串after中的任何位置。如果您将if条件更改为if (i != lowerCase.indexOf(characters[i]) || i != lowerCase.lastIndexOf(characters[i]))

,则可以修复此问题

固定版本的输出:3 (n,h,r)

答案 3 :(得分:0)

我建议使用Set仅保留唯一身份,然后计算其大小,而不是迭代:

public static int countUniqueCharacters(String s) {
    String lowerCase = s.toLowerCase();
    char characters[] = lowerCase.toCharArray();
    Set<Character> uniques = new HashSet<Character>();
    for (char c: characters) {
        uniques.add(c);
    }
    return uniques.size();
}

答案 4 :(得分:0)

if (i != lowerCase.indexOf(characters[i])) {
    countOfUniqueChars--;
}

这是错误的。你的lowerCase字符串是小写的,因此字符[i]中的任何大写字母在lowerCase中的索引为-1(将被计算为非唯一字符)。您可以使用indexOf(lowerCase.charAt(i));

来解决此问题

答案 5 :(得分:0)

计算字符数的好方法是消除重复。 ideia是获取第一个字符,然后查找下一个出现并替换为空,一旦你这样做,你可以计算唯一的字符。

public static int countUniqueCharacters(String s) {
    String lowerCase = s.toLowerCase();

    ///Get the first char of lowerCase
    String firstChar = lowerCase.substring(0,1);
    //Take off the first char
    String subS = lowerCase.substring(1);
    ///replace all chars equals to first char
    String replacedSubS = subS.replace(firstChar, "");

    /// Now, call method again to calculate size
    /// of the substring with first char
    // replaced by blank char
    return  1+countUniqueCharacters(replacedSubS);
}

这个方法适合我,看一看。你可以用两行来做,但我认为最好在这里详细说明。

答案 6 :(得分:0)

  

System.out.println(countOfUniqueChars);下面添加return不起作用。

这是预期的行为,因为return意味着控制流将从方法返回到调用此方法的位置。这意味着return之后的代码将不会被执行,因此在

之类的情况下
return countOfUniqueChars;
System.out.println(countOfUniqueChars);

System.out.println(countOfUniqueChars);将是死代码

您可以在返回之前尝试打印值,如

System.out.println(countOfUniqueChars);
return countOfUniqueChars;

或只是在主方法中打印返回值,如

int count = countUniqueCharacters(s);
System.out.println(count);

或使用此单行

System.out.println(countUniqueCharacters(s));

BTW,因为Java 8你的代码看起来像

s.toLowerCase().chars().distinct().summaryStatistics().getCount()

或者如果您想跳过空格,可以添加

s.toLowerCase().replace(" ","").chars().distinct().summaryStatistics().getCount()

答案 7 :(得分:0)

public static int countUniqueCharacters(String s) {
        char [] input=s.toCharArray();
        Set<Character> charset=new HashSet<>();
        for (int i = 0; i < input.length; i++) {
            charset.add(input[i]);
        }
        return charset.size();
    }
相关问题