In: Computer Science
Write an Algorithm that asks the user to enter an address and print each part of the address on a separate line.
Example:
Input
344 Rose Dr, Raeford, NC 28376
Output
Street No.: 344
Street Name: Rose Dr
City: Raeford
State: North Carolina
Zip Code: 28376
Hints:
Deal with the address as a string. The address must contain two commas. The text before first comma contains the street number and street name separated by one or more white spaces. The text after the second comma contains the state code and the zip code separated by one or more white spaces. The text between the two commas is the city name. The algorithm must print the state name instead of the state code.
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter full address");
String address = sc.nextLine();
String[] commaDiv = address.split(","); //Split the
address by , into 3 parts
String st = commaDiv[0]; // Street details
String city = commaDiv[1]; // City Name
String zip = commaDiv[2]; // State code and Zip
Code
String[] strName = st.split(" +"); //Street details
splitted by space/Multiple spaces
String strNumber = strName[0]; //First part as Street
Number
System.out.println("Street No.: "+ strNumber);
String street = ""; //Remaining is concatenated again
as Street Name
for(int i=1;i<strName.length;i++){
street = street + " " + strName[i];
}
System.out.println("Street:"+
street);
System.out.println("City: "+
city);
String zipTrimed = zip.trim();
//Trim the 3rd part - Remove spaces from begining and end
String[] zips = zipTrimed.split("
+"); //Split by spaces/Multiple spaces
String stateCode = zips[0]; //First
part as state Code
String zipCode = zips[1]; //Second
part as Zip Code
String state; //Find the state Name
by using State Code
switch(stateCode) {
case "AL":
state = "Alabama";
break;
case "AK":
state = "Alaska";
break;
case "AZ":
state = "Arizona";
break;
case "AR":
state = "Arkansas";
break;
case "CA":
state = "California";
break;
case "CO":
state = "Colorado";
break;
case "CT":
state = "Connecticut";
break;
case "DE":
state = "Delaware";
break;
case "DC":
state = "District of
Columbia";
break;
case "FL":
state = "Florida";
break;
case "GA":
state = "Georgia";
break;
case "HI":
state = "Hawaii";
break;
case "ID":
state = "Idaho";
break;
case "IL":
state = "Illinois";
break;
case "IN":
state = "Indiana";
break;
case "IA":
state = "Iowa";
break;
case "KS":
state = "Kansas";
break;
case "KY":
state = "Kentucky";
break;
case "LA":
state = "Louisiana";
break;
case "ME":
state = "Maine";
break;
case "MD":
state = "Maryland";
break;
case "MA":
state = "Massachusetts";
break;
case "MI":
state = "Michigan";
break;
case "MN":
state = "Minnesota";
break;
case "MS":
state = "Mississippi";
break;
case "MO":
state = "Missouri";
break;
case "MT":
state = "Montana";
break;
case "NE":
state = "Nebraska";
break;
case "NV":
state = "Nevada";
break;
case "NH":
state = "New Hampshire";
break;
case "NJ":
state = "New Jersey";
break;
case "NM":
state = "New Mexico";
break;
case "NY":
state = "New York";
break;
case "NC":
state = "North Carolina";
break;
case "ND":
state = "North Dakota";
break;
case "OH":
state = "Ohio";
break;
case "OK":
state = "Oklahoma";
break;
case "OR":
state = "Oregon";
break;
case "PA":
state = "Pennsylvania";
break;
case "RI":
state = "Rhode Island";
break;
case "SC":
state = "South Carolina";
break;
case "SD":
state = "South Dakota";
break;
case "TN":
state = "Tennessee";
break;
case "TX":
state = "Texas";
break;
case "UT":
state = "Utah";
break;
case "VT":
state = "Vermont";
break;
case "VA":
state = "Virginia";
break;
case "WA":
state = "Washington";
break;
case "WV":
state = "West Virginia";
break;
case "WI":
state = "Wisconsin";
break;
case "WY":
state = "Wyoming";
break;
default : state = stateCode;
}
System.out.println("State: " + state);
System.out.println("Zip Code: " + zipCode);
}
}
TestCase1 : 344 Rose Dr, Raeford, NC 28376
TestCase2 : 23 New Villa Street, Channai, CA 854769
Description :
1) First take the input string (address) from user.
2) Now split it by comma (,). It will be splitted into 3 parts.
i. First part : Street Number + Street Name
ii. Second Part : City Name
iii. Third Part : StateCode + ZipCode
3) Now split the first part by spaces/multiple spaces. Th first part will be the StreetNumber and the remaining part will be the Street name, so we will again concatenate the remaining part to get the Street Name
4) Second part will be City as it is.
5) Third part will be trimmed first to remove the white spaces from the beginning and the ending of the string. (this step also can be done for first 2 parts)
Now split this part by spaces/multiple spaces. First part we will get the stateCode and second as ZipCode.
6) To get the stateName from the stateCode, i have hardcoded the values. You can also use the another file which returns names from stateCode.
Thus we will get the proper output as desired.