8.5. Command-Line Arguments

 
[Page 270 ( continued )]

8.3. The Character Class

Java provides a wrapper class for every primitive data type. These classes are Character , Boolean , Byte , Short , Integer , Long , Float , and Double for char , boolean , byte , short , int , long , float , and double . All these classes are in the java.lang package. They enable the primitive data values to be treated as objects. They also contain useful methods for processing primitive values. This section introduces the Character class. The other wrapper classes will be introduced in Chapter 10, "Abstract Classes and Interfaces."


[Page 271]

The Character class has a constructor and several methods for determining a character's category (uppercase, lowercase, digit, etc.) and for converting characters from uppercase to lowercase, and vice versa, as shown in Figure 8.6.

Figure 8.6. The Character class provides the methods for manipulating a character.

You can create a Character object from a char value. For example, the following statement creates a Character object for the character 'a' .

 Character character =   new   Character(   'a'   ); 

The charValue method returns the character value wrapped in the Character object. The compareTo method compares this character with another character and returns an integer that is the difference between the Unicodes of this character and the other character. The equals method returns true if and only if the two characters are the same. For example, suppose charObject is new Character('b') :

 charObject.compareTo(   new   Character(   'a'   )) returns   1   charObject.compareTo(   new   Character(   'b'   )) returns     charObject.compareTo(   new   Character(   'c'   )) returns   1   charObject.compareTo(   new   Character(   'd'   ) returns   2   charObject.equals(   new   Character(   'b'   )) returns   true   charObject.equals(   new   Character(   'd'   )) returns   false   

Most of the methods in the Character class are static methods. The isDigit(char ch) method returns true if the character is a digit. The isLetter(char ch) method returns true if the character is a letter. The isLetterOrDigit(char ch) method is true if the character is a letter or a digit. The isLowerCase(char ch) method is true if the character is a lowercase letter. The isUpperCase(char ch) method is true if the character is an uppercase letter. The toLowerCase(char ch) method returns the lowercase letter for the character, and the toUpperCase(char ch) method returns the uppercase letter for the character.

8.3.1. Example: Counting Each Letter in a String

This example presents a program that prompts the user to enter a string and counts the number of occurrences of each letter in the string regardless of case, as shown in Figure 8.7.


[Page 272]
Figure 8.7. The program counts the number of occurrences for each letter in the string.

Here are the steps to solve this problem:

1.
Convert all the uppercase letters in the string to lowercase using the toLowerCase method in the String class.

2.
Create an array, say counts of twenty-six int values, each of which counts the occurrences of a letter. That is, counts[0] counts the number of a 's, counts[1] counts the number of b 's, and so on.

3.
For each character in the string, check whether it is a (lowercase) letter. If so, increment the corresponding count in the array.

Listing 8.2. CountEachLetter.java
 1   import   javax.swing.JOptionPane;  2  3   public class   CountEachLetter {  4  /**  Main method */  5   public static void   main(String[] args) {  6  // Prompt the user to enter a string  7     String s = JOptionPane.showInputDialog(   "Enter a string:"   );  8  9  // Invoke the countLetters method to count each letter  10   int   [] counts = countLetters(s.toLowerCase()); 11 12  // Declare and initialize output string  13     String output =   ""   ; 14 15  // Display results  16   for   (   int   i =     ; i < counts.length; i++) { 17   if   (counts[i] !=     ) 18         output += (   char   )(   'a'   + i) +   " appears "   + 19           counts[i] + ((counts[i] ==   1   ) ?   " time\n"   :   " times\n"   ); 20     } 21 22  // Display the result  23     JOptionPane.showMessageDialog(   null   , output); 24   } 25 26  // Count each letter in the string  27   public static int   [] countLetters(  Sring s  ) { 28   int   [] counts =   new int   [   26   ]; 29 30   for   (   int   i =     ; i <  s.length()  ; i++) { 31   if   (  Character.isLetter(s.charAt(i))  ) 32         counts[s.charAt(i) -   'a'   ]++; 33     } 34 35   return   counts; 36   } 37 } 


[Page 273]

The main method reads a string (line 7) and counts the number of occurrences of each letter in the string by invoking the countLetters method (line 10). Since the case of the letters is ignored, the program uses the toLowerCase method to convert the string into all lowercase and pass the new string to the countLetters method.

The countLetters method (lines 27 “36) returns an array of twenty-six elements. Each element counts the number of occurrences of a letter in the string s . The method processes each character in the string. If the character is a letter, its corresponding count is increased by 1. For example, if the character ( s.charAr(i) ) is 'a' , the corresponding count is counts['a' - 'a'] (i.e., counts[0] ). If the character is 'b' , the corresponding count is counts['b' - 'a'] (i.e., counts[1] ) since the Unicode of 'b' is 1 more than that of 'a' . If the character is 'z' , the corresponding count is counts['z' - 'a'] (i.e., counts[25] ) since the Unicode of 'z' is 25 more than that of 'a' .

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net