This program should count the number of characters entered by the user. Where are other other characters such as !, @, $, etc. He should not count #. The following for this is my code:
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 the "other" counter. If I log in !, @ And $, it will only count 2 out of 3 characters entered. What's wrong?
source share