Jump to content

Java program that counts the number of specific types of characters entered by user

- - - - -

  • Please log in to reply
2 replies to this topic

#1
tabs

tabs

    Newbie

  • Members
  • Pip
  • 1 posts
I have no idea what im doing.

Thanks for the help in advance :)

Attached Files



#2
nicckk

nicckk

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 629 posts
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
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
There's an easier way. Just use:


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