When running the following program and inputing a letter, one of the output windows says that the letter is a digit when it is clearly not. Why?
import javax.swing.JOptionPane;
/**
* This program demonstrates some of the Character
* class's character testing methods
*
*
*/
public class CharacterTest {
public static void main(String[] args){
String input; //To hold the user's input
char ch; //To hold a single character
//Get a character from the user and store
//it in the ch variable
input=JOptionPane.showInputDialog("Enter "+
"any single character.");
ch= input.charAt(0);
//Test the character
if(Character.isLetter(ch)){
JOptionPane.showMessageDialog(null, "This is a letter.");
}
if(Character.isDigit(ch));{
JOptionPane.showMessageDialog(null, "Thit is a digit.");
}
if(Character.isLowerCase(ch)){
JOptionPane.showMessageDialog(null, "That is a lowercase"+
" letter");
}
if(Character.isUpperCase(ch)){
JOptionPane.showMessageDialog(null, "That is an uppercase"+
" letter");
}
if(Character.isSpaceChar(ch)){
JOptionPane.showMessageDialog(null, "That is an uppercase"+
" letter");
}
if(Character.isWhitespace(ch)){
JOptionPane.showMessageDialog(null, "That is an uppercase"+
" letter");
}
System.exit(0);
}
}
That means:
without any condition, so it will always print that it’s a digit.
By the way, neither spaces nor white spaces (funny how Java distinguishes between the two) are “uppercase letters”.