Assignment
1) Write a program that prompts the user to enter some input, which can be any sequence of arbitrary characters, such as 1er$^ #sdL';[3oD. Use a for-loop to iterate through each character in the input. Within the for-loop, use conditional logic (if-else if or switch) along with appropriate methods from the Character class (hint: look at the methods beginning with "is" to identify each character and determine whether it is:
When you run your program, it should produce output
similar to that below (using the input above as an example). Hint: the format
specifier needed to output a character is %c; to output a string, use %s.
Enter some input (anything): 1er$^ #sdL';[3oD
You entered 1er$^ #sdL';[3oD, which contains 16 characters. At index 0, the character 1 is a digit. At index 1, the character e is a lowercase letter. At index 2, the character r is a lowercase letter. At index 3, the character $ is something else. At index 4, the character ^ is something else. At index 5, the character is white space. At index 6, the character # is something else. At index 7, the character s is a lowercase letter. At index 8, the character d is a lowercase letter. At index 9, the character L is an uppercase letter. At index 10, the character ' is something else. At index 11, the character ; is something else. At index 12, the character [ is something else. At index 13, the character 3 is a digit. At index 14, the character o is a lowercase letter. At index 15, the character D is an uppercase letter.
Done.
Solution
/** * HW6 Solution * 10/2009 */ package forExamples; import java.util.Scanner; /** * @author hornick * This program analyzes the characters in a String */ public class HW6 { /** * @param args */ public static void main(String[] args) { System.out.print("Enter some input (anything): " ); Scanner kbdReader = new Scanner( System.in ); String input = kbdReader.nextLine(); int length = input.length(); System.out.printf("You entered %s, which contains %5d characters. \n", input, length ); for( int index = 0; index< input.length(); index++ ) { // Note: read sections 5.4 and 5.5 of the textbook again, regarding "wrapper classes" // for primitive datatypes. The String.charAt method actually returns a char datatype, // but I'm using a Character datatype (Character is a wrapper class for char) to // contain the return value. I can use a Character where a char is expected because Java // implements a convenience feature called "boxing" and "unboxing". I need to use the // Character class because it has methods that I can call. Primitive datatypes like char // are not classes and thus don't have any methods. Character c = input.charAt(index); // get the character at the specified index System.out.printf("At index %3d, the character %c is ", index, c); // echo it if( Character.isDigit(c) ) { // is the character a numerical digit? System.out.printf("a digit. \n" ); } else if( Character.isLowerCase(c) ) { // is it lowercase? System.out.printf("a lowercase letter. \n" ); } else if( Character.isUpperCase(c) ) { // is it uppercase? System.out.printf("an uppercase letter. \n" ); } else if( Character.isWhitespace(c) ) { // is it a space or tab? System.out.printf("white space. \n" ); } else { // it's something else System.out.printf("something else. \n" ); //NOTE: There are also lots of additional Character class methods we can invoke if // we wanted to determine more specifically what the character is. See, for // example, the methods isLetter() or isJavaIdentifierStart(). } // end while } } }