Java Reference
In-Depth Information
if(symbol <= 'Z') { // yes, and is it Z or less?
// Then it is a capital letter
System.out.println("You have the capital letter " + symbol);
} else { // It is not Z or less
if(symbol >= 'a') { // So is it a or greater?
if(symbol <= 'z') { // Yes, so is it z or less?
// Then it is a small letter
System.out.println("You have the small letter " + symbol);
} else { // It is not less than z
System.out.println(
"The code is greater than a but it's not a letter");
}
} else {
System.out.println(
"The code is less than a and it's not a letter");
}
}
} else {
System.out.println("The code is less than A so it's not a letter");
}
}
}
How It Works
This program figures out whether the character stored in the variable symbol is an uppercase letter, a
lowercase letter, or some other character. The program first generates a random character with a
numeric code between 0 and 127, which corresponds to the characters in the basic 7-bit ASCII (ISO
646) character set. The Unicode coding for the ASCII characters is numerically the same as the ASCII
code values. Within this character set, the letters ' A ' to ' Z ' are represented by a contiguous group of
ASCII codes with decimal values from 65 to 90. The lowercase letters are represented by another
contiguous group with ASCII code values that have decimal values from 97 to 122. So to convert any
capital letter to a lowercase letter, you just need to add 32 to the character code.
The if statements are a bit convoluted so let's look at a diagram of the logic.
Search WWH ::




Custom Search