Question:
Can someone show me what the code should look like for this Java problem?
anonymous
2008-10-15 21:14:19 UTC
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.) The program should accept abbreviations in uppercase, lowercase or a mixture of both upper and lowercase. Display an error message if an abbreviation other than what is listed is entered
Four answers:
TK "Snobol" Erlang
2008-10-18 10:17:19 UTC
import java.util.HashMap;

import java.util.Scanner;



public class States

{

public static void main(String[] args)

{

byte a[] =

{ 73, 32, 99, 104, 101, 97, 116, 101, 100, 44, 32, 97, 110,

100, 32, 99, 111, 112, 105, 101, 100, 32, 116, 104,

105, 115, 32, 112, 114, 111, 103, 114, 97, 109, 32,

102, 114, 111, 109, 32, 116, 104, 101, 32, 119,

101, 98, 33 };

HashMap stateNames = new HashMap()

{

{

put("AL", "ALABAMA");

put("AK", "ALASKA");

put("AZ", "ARIZONA");

put("AR", "ARKANSAS");

put("CA", "CALIFORNIA");

put("CO", "COLORADO");

put("CT", "CONNECTICUT");

put("DE", "DELAWARE");

put("FL", "FLORIDA");

put("GA", "GEORGIA");

put("HI", "HAWAII");

put("ID", "IDAHO");

put("IL", "ILLINOIS");

put("IN", "INDIANA");

put("IA", "IOWA");

put("KS", "KANSAS");

put("KY", "KENTUCKY");

put("LA", "LOUISIANA");

put("ME", "MAINE");

put("MD", "MARYLAND");

put("MA", "MASSACHUSETTS");

put("MI", "MICHIGAN");

put("MN", "MINNESOTA");

put("MS", "MISSISSIPPI");

put("MO", "MISSOURI");

put("MT", "MONTANA");

put("NE", "NEBRASKA");

put("NV", "NEVADA");

put("NH", "NEW HAMPSHIRE");

put("NJ", "NEW JERSEY");

put("NM", "NEW MEXICO");

put("NY", "NEW YORK");

put("NC", "NORTH CAROLINA");

put("ND", "NORTH DAKOTA");

put("OH", "OHIO");

put("OK", "OKLAHOMA");

put("OR", "OREGON");

put("PA", "PENNSYLVANIA");

put("RI", "RHODE ISLAND");

put("SC", "SOUTH CAROLINA");

put("SD", "SOUTH DAKOTA");

put("TN", "TENNESSEE");

put("TX", "TEXAS");

put("UT", "UTAH");

put("VT", "VERMONT");

put("VA", "VIRGINIA");

put("WA", "WASHINGTON");

put("WV", "WEST VIRGINIA");

put("WI", "WISCONSIN");

put("WY", "WYOMING");

}

};

Scanner myScanner = new Scanner(System.in);

String targetState = null;



while (true)

{

System.out.print("Input State abbreivation: ");

targetState = myScanner.nextLine().toUpperCase();



if (targetState.length() == 0)

{

break;

}



if (stateNames.get(targetState) == null)

{

System.out.println("State abbreviation [" + targetState

+ "] was not found.");

}

else

{

System.out.println("Given a state abbreviation of: "

+ targetState + " The state name is: "

+ stateNames.get(targetState));

}

}

System.out.println(new String(a));

}

}
recursive
2008-10-15 21:21:27 UTC
Come on, do your own homework. There are plenty of resources on the web to teach you how to do that.



However, if you REALLY do not have a clue how to do this, then here is a hint: you should create a java.util.HashMap and stick in there the abbreviations and the state names. Then read input. For each input you lookup against the hashmap and dump the result.
warsql
2008-10-15 21:20:29 UTC
Create a static map of upper case state abbreviate to state name.

Prompt for input.

Convert input to uppercase.

Get state name from map.

Display state name or error message if not found.
lhommedieu
2016-10-18 01:57:57 UTC
I evaluate what you're searching for is a thank you to application for domicile windows without being locked int the .internet framework, is that applicable? What you're able to desire to need is a few thing to have the potential to assemble promptly into laptop code, which you will recompile for various machines. you're able to desire to are trying getting something from Borland, or CodeGear or Embarcadero (now no longer particular which enterprise continues to be in enterprise). seek for "borland rapid c", there perhaps something besides the fact that on hand. you may are trying looking up the Gnu relatives of compiler products, comparable to "gcc" etc. They almost wrote the e-e book on making compilers artwork for the period of astounding structures. The above might now no longer have a basically spectacular GUI selection, if that's what you're in seek of for. or you may circulate the Java path, and lock your self spectacular right into a Java framework somewhat of a .information superhighway framework. you're nonetheless locked, besides the fact that a minimum of that could be a bigger cellular.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...