Pergunta

// I have a program where I am supposed to count unique characters, only letters and numbers and don't count repeating numbers or letters. However, I have a problem finding out a way for the program not to count spaces and symbols such as "!" "@" "#" "$". So If i type in Hello! I only want the program to say "4", but it says "5" because it counts the exclamation point. Here is my code so far:

public static int countUniqueCharacters(String text1) {
        int count = 0;
        for (int i = 0; i < text1.length(); i++) {
            if (text1.substring(0, i).contains(text1.charAt(i) + ""))
                System.out.println();
            else
                count++;
        }
        return count;

    }
Foi útil?

Solução

In your else block add a condition that the count will be incremented only if the given character is a letter or a digit.

if (Character.isLetter(text1.charAt(i)) || Character.isDigit(text1.charAt(i))) {
    count++;
}

In your example:

public static int countUniqueCharacters(String text1) {
    int count = 0;
    for (int i = 0; i < text1.length(); i++) {
        if (text1.substring(0, i).contains(text1.charAt(i) + "")) {
            System.out.println();
        } else if (Character.isLetter(text1.charAt(i)) || Character.isDigit(text1.charAt(i))) {
            count++;
        }
    }
    return count;
}

Outras dicas

here's a sample code written in C# try and understand it. It compares with ascii and adds in a list

string input = Console.ReadLine();//input
        List<char> CountedCharacters = new List<char>();
        for (int i = 0; i < input.Length; i++)
        {           //checking for numerics             //checking for alphabets uppercase      //checking for alphabets lowercase
            if ((input[i] >= 45 && input[i] <= 57) || (input[i] >= 65 && input[i] <= 90) || (input[i] >= 97 && input[i] <= 122))
            {
                bool AlreadyExists = false;
                for (int j = 0; j < CountedCharacters.Count; j++)
                {
                    ////checking if already exists
                    if (CountedCharacters[j]==input[i])
                    {
                        AlreadyExists = true;
                        break;
                    }
                }
                ////adding in list if doesnt exists
                if (!AlreadyExists)
                {
                    CountedCharacters.Add(input[i]);
                }

            }
        }


        for (int i = 0; i < CountedCharacters.Count; i++)
        {
            Console.WriteLine(CountedCharacters[i]);
        }

Try this one using regex. You can add and remove the characters you need from the expression to count what you need.

public static int countUniqueCharacters(String text1) {
    String newText = text1.replaceAll("[^A-Za-z0-9()\\[\\]]", "");
    Set<Character> tempSet = new HashSet<>();
    for (char item : newText.toCharArray()) {
        tempSet.add(item);
    }
    return tempSet.size();
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top