8. Java Data and Operators

8.8. Example: Character Conversions

 

Another interesting implication of representing the characters as integers is that we can represent various character operations in terms of inte- ger operations. For example, suppose we want to capitalize a lower-

case letter. Table 5.13 shows that the entire sequence of lowercase let- Lowercase to uppercase

ters (’a’ ... ’z’) is displaced by 32 from the sequence of uppercase letters (’A’ ... ’Z’), so we can convert any lowercase letter into its corresponding uppercase letter by subtracting 32 from its integer value, provided we perform an explicit cast on the result. When we perform the cast (char) (’a’ - 32 ), the resulting value is ’A’, as the following example shows:

,,

 

J

 

Recall that in evaluating ’a’ - 32 Java will promote ‘a’ to an int and then perform the subtraction. Thus, a step-by-step evaluation of the ex- pression would go as follows:

,,

 

 

 

 

Uppercase to lowercase


J

Similarly, we can convert an uppercase letter into the corresponding low- ercase letter by simply adding 32 to its integer code and casting the result back to a char:

 

,,

 

J

We can group these ideas into a method that performs conversion from lowercase to uppercase:

,,

 

 

 

 

 

 

 

Type error


J

This method takes a single char parameter and returns a char value. It begins by checking if ch is a lowercase letter—that is, if ch falls between ‘a’ and ‘z’ inclusive. If so, it returns the result of subtracting 32 from ch. If not, it returns ch unchanged. However, the method contains a syntax error that becomes apparent if we trace through its steps. If we invoke it with the expression toUpperCase(’b’), then since ‘b’ is between ‘a’ and ‘z’, the method will return ‘b’ 32. Because the integer value of ‘b’ is 98, it will return 98 32 or 66, which is the integer code for the character ‘B’. However, the method is supposed to return a char, so this last statement will generate the following syntax error:

,,

 

 

 

 

J

In order to avoid this error, the result must be converted back to char

before it can be returned:

,,

 

 

 

 

J

 

SECTION 5.9 Problem Solving = Representation + Action237

Another common type of conversion is to convert a digit to its correspond- ing integer value. For example, we convert the character ‘9’ to the integer

9 by making use of the fact that the digit ‘9’ is 9 characters beyond the digitDigit to integer

‘0’ in the lexical order. Therefore, subtracting ‘0’ from ‘9’ gives integer 9 as a result:

,,

 

J

More generally, the expression ch ‘0’ will convert any digit, ch, to its integer value. We can encapsulate these ideas into a method that converts any digit into its corresponding integer value:

,,

 

 

 

J

This method takes a single char parameter and returns an int. It first checks that ch is a valid digit, and if so, it subtracts the character ‘0’ from

it. If not, the method just returns 1, which indicates that the method received an invalid input parameter. Obviously, when an object invokes this method, it should first make sure that the value it passes is in fact a digit.

The Java application program shown in Figure 5.12 illustrates sev- eral of the ideas discussed in this section. Note that both the digitToInteger() and toUpperCase() are declared static. This al- lows us to call them directly from the (static) main() method, as useful and justifiable shortcut if, as in this case, we are just testing the methods.