Reputation: 11
I am new to programming and Java. I am having a problem which is to make my program recognize Uppercase letters only and display error message if the user input is non-alphabetic character. I am instructed to use boolean and strings for this problem. Thanks!
Scanner in = new Scanner(System.in);
System.out.println("Enter a single letter, and I will tell you what the corresponding digit is on the telephone:");
String letter = in.nextLine();
String upperCase = letter.toUpperCase();
boolean letter = letter.hasUpperCase();
if (letter.equals("A") || letter.equals("B") || letter.equals("C")){
System.out.println("The digit 2 corresponds to the letter " + letter + " on the telephone.");
}
else if{
System.out.println("Please enter a letter.")
}
Upvotes: 1
Views: 2968
Reputation: 5294
If you want to check for only uppercase letters then one approach would be to check each character's ASCII value to see if it falls in the range between 65 and 90. If all characters in the string meet this criterion then you have a string containing only A-Z. If not, you can then show an error message as appropriate. See the example below based on your code.
Scanner in = new Scanner(System.in);
System.out.println("Your message here");
String userInput = in.nextLine();
in.close();
// Here you don't know what they entered
// but looking at your code, you want
// only ONE uppercase character
// validate the input
boolean validInput = false;
if (userInput.length() == 1) {
char letter = userInput.charAt(0);
if (letter >= 65 && letter <= 90) {
validInput = true;
}
}
// handle accordingly
if (validInput) {
// your logic here
} else {
System.out.println("Your error message");
}
EDIT -- what to do to check if the character is a number or a symbol
...
// Here you don't know what they entered
// the validation / execution changes based on your needs
// suggestion: define other methods
if (userInput.length() == 1) {
char c = userInput.charAt(0);
// what kind of character is it?
if (isUppercaseLetter(c)) {
// do something with the uppercase letter
} else if (isDigit(c)) {
// do something with the digit
} else if (isSymbol(c)) {
// do something with the symbol
} else {
// whatever else it might be, this could be an error message
}
}
...
// methods here
private boolean isUppercaseLetter(char c) {
return (c >= 'A' && c <= 'Z'); // ascii codes 65 to 90
}
private boolean isDigit(char c) {
return (c >= '0' && c <= '9'); // ascii codes 30 to 39
}
private boolean isSymbol(char c) {
return (c >= '!' && c <= '/'); // 21 to 47, modify based on acceptable symbols
}
Upvotes: 1
Reputation:
I'm not sure what you ask. But I modify your code, so it can run based on my way of looking at it.
Scanner in = new Scanner(System.in);
System.out.println("Enter a single letter, and I will tell you what the corresponding digit is on the telephone:");
String letter = in.nextLine();
boolean hasUpper = false;
if (letter.equals("A") || letter.equals("B") || letter.equals("C")){
hasUpper = true;
System.out.println("The digit 2 corresponds to the letter " + letter + " on the telephone.");
}
else if(letter.equals("a") || letter.equals("b") || letter.equals("c")){
System.out.println("Error! The letter has to be uppercase.");
}
else{
System.out.println("Please enter a letter.");
}
Upvotes: 0