|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Java newbie needs help
Hi,
I just started learning java a week ago. I wrote a program that asks to input a character and it outputs the unicode number for it. I would like to know how to make it so the program shows an error message if the character entered by the user is not a letter(capital or lowercase) or a digit. Any help would be great. thanks Heres my code so far public class UnicodeConversion { public static void main(String[]args) { char Character; int Unicode; System.out.println("Please enter a character of your choice."); // Ask for the input of a character Character = SavitchIn.readLineNonwhiteChar(); // Convert character to unicode Unicode = Character; //Output result System.out.println("You entered character " + Character + "."); System.out.println("The Unicode value of Character " + Character + " is " + Unicode + "."); } } |
|
#2
|
|||
|
|||
|
well you could do
---------------------------------------------- if (Charactor.equalsIgnoreCase("a") || Charactor.equalsIgnoreCase("b") || Charactor.equalsIgnoreCase("c") || Charactor.equalsIgnoreCase("d") || ... Charactor.equalsIgnoreCase("z")) { // do nothing } else { system.out.println("you did something horribly wrong") } ----------------------------------------------- or something along those lines. (syntax may not be correct). hope that helps. Feel free to ask more questions, though do not get disapointed if no one answers, i'm still waiting for someone to tell me about my problem -_- |
|
#3
|
|||
|
|||
|
Thanks for the reply, but the code is going to be very long if I have to do it for every single character and digit. Is it possible to have the same result but typing less code?
thanks |
|
#4
|
|||
|
|||
|
Character testing
Try these methods from the Character class out...
Character.isLetter (char ch) Character.isDigit (char ch) Character.isLetterOrDigit (char ch) These will return a boolean (true or false) and should get you what you want. Have a good one. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > Java newbie needs help |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|