Thanks for the help in advance :)
Java program that counts the number of specific types of characters entered by user
Started by tabs, Mar 03 2011 11:26 AM
2 replies to this topic
#1
Posted 03 March 2011 - 11:26 AM
I have no idea what im doing.
Thanks for the help in advance :)
Thanks for the help in advance :)
|
|
|
#2
Posted 03 March 2011 - 05:29 PM
You could do this by comparing the inputed characters. After you ask for an input you will need to check what type of character was inputed with if statements, such as
if(inputChar >= 65 && inputChar <= 90) //65 = 'A' and 90 = 'Z' upperCase++; //An instance variable to store how many upperCase letters else if(inputChar >= 97 && inputChar <= 122) //97 = 'a' and 122 = 'z' lowerCase++; else if(inputChar >= 48 && inputChar <= 57) // 48 = '0' and 57 = '9' numDigits++; else otherLetters++;
#3
Posted 04 March 2011 - 02:33 PM
There's an easier way. Just use:
The result will be in the form of an integer which corresponds with several static integer members of the Character class, for example:
The list goes on and on. Just compare the result of the getType() function with these static members in a switch clause and you'll be good to go.
Google the Java documentation on Character.getType() for all the possible character categories.
int type = Character.getType(c); // where c is a character to test
The result will be in the form of an integer which corresponds with several static integer members of the Character class, for example:
Character.LOWERCASE_LETTER Character.UPPERCASE_LETTER Character.MATH_SYMBOL Character.DECIMAL_DIGIT_NUMBER Character.START_PUNCTUATION Character.END_PUNCTUATION ...
The list goes on and on. Just compare the result of the getType() function with these static members in a switch clause and you'll be good to go.
Google the Java documentation on Character.getType() for all the possible character categories.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









