The program asks a user to enter one of the following state abbreviations: NC, SC, GA, FL, or AL. Then the state name of the abbreviation entered is displayed. The program is supposed to accept abbreviations in uppercase, lowercase, or a mixture of both upper and lowercase. The program complies but something is still wrong. For example, if the user enters NC, then the state name North Carolina is displayed, which is correct. And if the user enters something like ZC, then the error message is displayed, which is correct. However, if the user enters something like Na, then North Carolina is displayed, which is wrong. So can anyone tell me what I'm doing wrong? Here is my code:
StateAbbr.java
import java.util.Scanner; // Needed for Scanner class
/***************************************************************
* Purpose: This program asks the user to enter one of the
* following state abbreviations: NC, SC, GA, FL, or AL.
* Then displays the name of that state. An error message is
* displayed if an abbreviation other than what is listed
* is entered.
*
**************************************************************/
public class StateAbbr
{
public static void main(String[] args)
{
String input; // Keyboard input
char choice; // User's choice
// Create a Scanner object to read input
Scanner keyboard = new Scanner(System.in);
// Ask the user to enter NC, SC, GA, FL, or AL
System.out.print("Enter NC, SC, GA, FL, or AL: ");
input = keyboard.nextLine();
// Get the first char
choice = input.charAt(0);
// Determine which character the user entered
switch(choice)
{
case 'n':
case 'N':
System.out.println("North Carolina.");
break;
case 's':
case 'S':
System.out.println("South Carolina.");
break;
case 'g':
case 'G':
System.out.println("Georgia.");
break;
case 'f':
case 'F':
System.out.println("Florida.");
break;
case 'a':
case 'A':
System.out.println("Alabama.");
break;
default:
System.out.println("Invalid choice.");
}
}
}
This post has been edited by Alleluia707: 15 November 2008 - 04:48 PM

New Topic/Question
Reply




MultiQuote




|