Page 155 - Introduction to Programming with Java: A Problem Solving Approach
P. 155

                4.7 switch Statement 121 The first digit of a U.S. postal ZIP Code identifies a particular geographic area within the United States. ZIP
Codes that start with 0, 2, or 3 are in the east, ZIP Codes that start with 4, 5, or 6 are in the central region,
3
and so on. Your program should prompt the user for his/her ZIP Code and use the first character of the
entered value to print the user’s geographical region. In addition to printing the geographical region, your program should echo print the user’s ZIP Code. (Echo print means print out an input exactly as it was read in.) Here’s an example of what the program should do:
Sample session:
Enter a ZIP Code: 56044
56044 is in the Central Plains area.
That’s the client’s view of the program. Now let’s look at the implementation view of the program—the problem solution. It’s shown in Figure 4.9.
Look at the controlling expression, (zip.charAt(0)). This evaluates to the first character in zip. As an alternative, you could have started by reading the first character into a separate variable (for example, firstChar), and then inserted that variable into the controlling expression. But because the first character was needed only at one point, the code is made more compact by embedding the zip.charAt(0) method call directly in the controlling expression’s parentheses.
The switch statement compares the character in its controlling expression with each of the case con-
stants until it finds a match. Since the controlling expression’s charAt method returns a char value, the
case constants must all be chars. Therefore, the case constants must have single quotes around them.
If you don’t use single quotes—if you use double quotes or no quotes—you’ll get a compile-time error. The
switch statement is not very flexible!
Apago PDF Enhancer
As previously mentioned, it’s a common error to accidentally omit a break statement at the end of a switch statement’s case block. For example, suppose you did this in the ZipCode program:
case '4': case '5': case '6':
System.out.println(
zip + " is in the Central Plains area.");
case '7':
System.out.println(zip + " is in the South.");
break;
Note that there’s no longer a break statement at the end of the case 4, 5, 6 block. The following sample session illustrates what happens. With an input of 56044, the switch statement searches for a ‘5’ and stops whenitreachesthecase '5':label.Executionbeginsthereandcontinuesuntilitreachesabreakstate- ment.Soitflowsthroughthecase '6':labelandprintstheCentralPlainsmessage.Theflowthencontin- ues into the case: '7': block and inappropriately prints the South message.
Sample session:
Enter a ZIP Code: 56044
56044 is in the Central Plains area.
56044 is in the South.
  Use client’s view to specify program.
    error!
  3 http://www.nass.usda.gov/census/census97/zipcode/zipcode.htm.




































































   153   154   155   156   157