Develop a Java application that inputs a two-letter abbreviation for one of the
ID: 3543794 • Letter: D
Question
Develop a Java application that inputs a two-letter abbreviation for one of the 50 states and displays the full name of the state. If the abbreviation isn't valid, the application should display an error and ask for the abbreviation again. The names of the 50 states and their abbreviations are given in the following table:
http://en.wikipedia.org/wiki/List_of_U.S._state_abbreviations
(Hint: use nested switch statements, where the outer statement uses the first letter of the abbreviation as its switch statement)
**Java 7 can also be used with Strings in the switch statement
Explanation / Answer
import java.util.Scanner;
public class node
{
public static void main(String args[])
{
String abbr[]={"US-AL","US-AK","US-AZ","US-AR","US-CA","US-CO","US-CT","US-DE","US-DC","US-FL","US-GA","US-HI","US-ID","US-IL","US-IN","US-IA","US-KS","US-KY","US-LA","US-ME","US-MD","US-MA","US-MI","US-MN","US-MS","US-MO","US-MT","US-NE","US-NV","US-NH","US-NJ","US-NM","US-NY","US-NC","US-ND","US-OH","US-OK","US-OR","US-PA","US-RI","US-SC","US-SD","US-TN","US-TX","US-UT","US-VT","US-VA","US-WA","US-WV","US-WI","US-WY" };
String state[]={"Alabama","Alaska","Arizona","Arkansas","California","Colorado","Connecticut","Delaware","District of Columbia","Florida","Georgia","Hawaii","Idaho","Illinois","Indiana","Iowa","Kansas","Kentucky","Louisiana","Maine","Maryland","Massachusetts","Michigan","Minnesota","Mississippi","Missouri","Montana","Nebraska","Nevada","New Hampshire","New Jersey","New Mexico","New York","North Carolina","North Dakota","Ohio","Oklahoma","Oregon","Pennsylvania","Rhode Island","South Carolina","South Dakota","Tennessee","Texas","Utah","Vermont","Virginia","Washington","West Virginia","Wisconsin","Wyoming"};
Scanner in=new Scanner(System.in);
System.out.println("Please enter the 2-letter abbreviation code LIKE US-CA");
String abr=in.nextLine();
int count=1;
while(count==1)
{
for(int i=0;i<abbr.length;i++)
{
if(abbr[i].equalsIgnoreCase(abr))
{
count=0;
System.out.println("State of entee code is:"+state[i]);
}
}
if(count==1){
System.out.println("Wrong code .Please try again and entered correct code LIKE US-CA");
abr=in.nextLine();
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.