2. Google an ASCII table and note that values from 48 to 122 represent characters in three categories; digits, special characters, or letters of the alphabet. Write a Java program that generates a random integer from 48 to 122, inclusive. Then use methods of the Character class in selections to detect and report the ASCII category of the character for that integer. Include case if the character is a letter. See example outputs below.
Example Outputs
The integer is 71
The ASCII character for that is G
That is an upper case letter
The integer is 61
The ASCII character for that is =
That is punctuation or a special character
The integer is 102
The ASCII character for that is f
That is a lower case letter
1
Expert's answer
2016-02-19T07:59:32-0500
import java.util.Random;
public class CharGenerate { public static void main(String[] args) { Random random = new Random(); int c; for(int i = 0; i < 5; i++) { c = random.nextInt(75) + 48; showCharInfo(c); System.out.println(); } }
public static void showCharInfo(int c) { char ch = (char) c; System.out.println("The integer is " + c); System.out.println("The ASCII character for that is " + (char)c); if(Character.isDigit(c)) System.out.println("That is digits"); else if(Character.isUpperCase(c)) System.out.println("That is an upper case letter"); else if(Character.isLowerCase(c)) System.out.println("That is a lower case letter"); else System.out.println("This is punctuation or a special character"); } }
Comments
Leave a comment