Switch Error

Program compiles but not correctly

Page 1 of 1

3 Replies - 2803 Views - Last Post: 15 November 2008 - 05:41 PM Rate Topic: -----

#1 Alleluia707  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-October 08

Switch Error

Post icon  Posted 15 November 2008 - 04:44 PM

This is supposed be a simple program but hey I'm a noob so I don't know what I'm doing.

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


Is This A Good Question/Topic? 0
  • +

Replies To: Switch Error

#2 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Switch Error

Posted 15 November 2008 - 04:54 PM

The switch statement is only checking the first chracter, which is N in both cases. As switch can only be used with the character, you will have to either use an if inside each switch to check second character or use an if statement instead of the switch
Was This Post Helpful? 1
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2675
  • View blog
  • Posts: 11,305
  • Joined: 20-September 08

Re: Switch Error

Posted 15 November 2008 - 04:56 PM

You would be better off reading String to get the user input
Was This Post Helpful? 0
  • +
  • -

#4 Alleluia707  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-October 08

Re: Switch Error

Posted 15 November 2008 - 05:41 PM

Alright, thanks a lot!

I used "if, else" and now the program compiles correctly :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1