The question ask:
Write a program that asks the user to enter one of the following state abbreviations: NC, SC, GA, FL, or AL. The program should then display the name of the state that corresponds with the abbreviation entered (North Carolina, South Carolina, Georgia, Florida, or Alabama).

This is what I have:

import java.util.Scanner;

class States
{
    public static void main(String [] args)
    {
        //variable declarations
        int state = 0;              //stores user input

        //establish a Scanner for input
        Scanner userInput = new Scanner(System.in);

        //prompt user to enter one of the following state
        //abbreviation: AL, FL, GA, NC, SC
        System.out.println("Please enter one of the following state abbreviation: AL, FL, GA, NC, or SC.");

                //get value from user and store in variable
                state = userInput.nextInt();

                //validate user input, making sure the state abbreviation is
                //either AL, FL, GA, NC, or SC
                while(state = AL, FL, GA, NC, SC)
                {

                    System.out.println("You entered an invalid abbreviation");
                    //prompt user to enter a valid state abbreviation
                    System.out.println("Please enter one of the following state abbreviation: AL, FL, GA, NC, or SC.");

                    //get value from user and store in variable
                    state = userInput.nextInt();
                }

                //user an if/else statement to display the correct state abbreviation
                        if(state = AL)
                        {
                            System.out.println(state + " = Alabama");
                        }
                        else if(state == FL)
                        {
                            System.out.println(state + " = Florida");

                        }
                        else if(state == GA)
                        {
                            System.out.println(state + " = Georgia");
                        }
                        else if(state == NC)
                        {
                            System.out.println(state + " = North Carolina");
                        }
                        else if(state == SC)
                        {
                            System.out.println(state + " = South Carolina");
                        }
                    }
                }

It says I have 4 errors. What is it that I'm doing wrong? Thanks!

Recommended Answers

All 2 Replies

the language is java by the way....

Examine this:

import java.util.Scanner;
import java.util.*;

class States2
{
	public static void main(String [] args)
	{
		Scanner userInput = null;
		String strStateAbbrev = "";

		String[] arr_strStatesAbbrev = 
			{"AL", "FL", "GA", "NC", "SC"};
		String[] arr_strStatesNames = 
			{"Alabama", "Florida", "Georgia", "North Carolina", "South Carolina"};

		Hashtable<String, String> map_s2sStates = new Hashtable<String, String>();

		for(int intLoop=0; intLoop < arr_strStatesAbbrev.length; intLoop++)
		{
			map_s2sStates.put(
				arr_strStatesAbbrev[intLoop], arr_strStatesNames[intLoop]);
		}

		StringBuilder sb = new StringBuilder();

		for(String s : arr_strStatesAbbrev)
		{
			sb.append(s + ", ");
		}

		String strListOfAbbr =
			sb.toString().substring(0, sb.toString().length() - 2);

		boolean blnMatched = false;
		do
		{
			System.out.println(
				"Please enter one of the following state abbreviation: "
				+ strListOfAbbr);

			userInput = new Scanner(System.in);

			strStateAbbrev = userInput.next().toUpperCase();

			if(map_s2sStates.containsKey(strStateAbbrev))
			{
				blnMatched = true;
				System.out.println(strStateAbbrev + " = " + map_s2sStates.get(strStateAbbrev));
			}

		} while(!blnMatched);
	}

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.