4

This program is supposed to count the number of characters entered by a user. Where other is other characters such as !, @, $, etc. It is not supposed to count #. The following is my code to do this:

public class countchars {
    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);

        char sym;
        int up = 0;
        int low = 0;
        int digit = 0;
        int other = 0;

        System.out.print("Enter a character # to quit: ");
        sym = input.next().charAt(0);

        while (sym != '#') {
            System.out.print("Enter a character # to quit: ");

            if (sym >= 'a' && sym <= 'z') {
                low++;
            }
            if (sym >= 'A' && sym <= 'Z') {
                up++;
            }
            if (sym >= '0' && sym <= '9') {
                digit++;
            }
            if (sym >= '!' && sym <= '=') {
                other++;
            }

            sym = input.next().charAt(0);

        }

        System.out.printf("Number of lowercase letters: %d\n", low);
        System.out.printf("Number of uppercase letters: %d\n", up);
        System.out.printf("Number of digits: %d\n", digit);
        System.out.printf("Number of other characters: %d\n", other);
    }
}

The problem is with the "other" counter. If I entered !, @, and $, it will only count 2 of the 3 characters entered. What's the wrong?

7 Answers 7

3

if you take a look at the ascii table, you'll see that:
'!' = 33
'=' = 61
'@' = 64

the '@' character isn't in the range you specified so it's not counted, replace the last condition with:

if (sym >= '!' && sym <= '@') {...}
Sign up to request clarification or add additional context in comments.

Comments

1

try this:

        if (sym >= 'a' && sym <= 'z') {
            low++;
        } else if (sym >= 'A' && sym <= 'Z') {
            up++;
        } else if (sym >= '0' && sym <= '9') {
            digit++;
        } else {
            other++;
        }

or instead of else you can select the short set of what that character can be:

        } else if ("%!$&".contains(sym)){
            other++;
        }

1 Comment

to add an explanation: the special symbols may be excluded from the ascii range of "!" and "="
1

Try with

else {
    other++;
}

instead of

if (sym >= '!' && sym <= '=') {
    other++;
}

# will not be counted as other because you already filter it in the while condition.

Comments

0

You should use OR(||) in condition instead of AND(&&)

if (sym == '!' || sym == '=' || sym == '@' || ...){
        other++;
}

Comments

0

To be sure you catch "everything else"; you just use an else clause. That way you don't miss things, like you're doing now (because '@' isn't in the range you're checking). You want this:

else {
    other++;
}

where you currently have this:

if (sym >= '!' && sym <= '=') {
    other++;
}

Comments

0

You compare char based on the ASCII value. @ ASCII is 64 ! ASCII is 33 = ASCII is 61

So @ isn't between "!" and "=" and doesn't increment your counter.

Comments

0

Look here, and hopefully the answer will present it self!

http://en.wikipedia.org/wiki/UTF-8

and are you sure the sequence $,5,$ will give you the right answer ? ;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.