Question

In: Computer Science

Write an Algorithm that asks the user to enter an address and print each part of...

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.

Solutions

Expert Solution

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.


Related Solutions

Write a program that asks the user to enter the total precipitation for each of the...
Write a program that asks the user to enter the total precipitation for each of the twelve months of the year into a list. The program should calculate and display the total precipitation for the year, the average monthly precipitation, and the months with the highest and lowest precipitation amounts. FYI: precipitation is measured in inches. Assume that precipitation amounts are floating-point numbers. You will need a list to store the precipitation amounts and another list to store the names...
Write a C program that asks the user to enter any two integernumbers, and each...
Write a C program that asks the user to enter any two integer numbers, and each number consists of four-digits. Your program should check whether the numbers are four digits or not and in case they are not a four digit number, the program should print a message and exit, otherwise it should do the following:Print a menu as follows:Select what you want to do with the number 1-3:1- Print Greatest Common Divisor (GCD) of the two numbers.2- Print sum...
1) Write Java application that asks the user to enter the cost of each apple and...
1) Write Java application that asks the user to enter the cost of each apple and number of apples bought. Application obtains the values from the user and prints the total cost of apples. 2) Write a java application that computes the cost of 135 apples, where the cost of each apple is $0.30. 3)Write a java application that prepares the Stationery List. Various entries in the table must be obtained from the user. Display the Stationary list in the...
Write a program that asks the user to enter the name of a file, and then...
Write a program that asks the user to enter the name of a file, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the file. Use Notepad or another text editor to create a sample file that can be used to test the program. Sample Run java FileLetterCounter Enter file name: wc4↵ Enter character to count: 0↵ The character '0' appears in the file...
Write a Java program that asks the user to enter an integer that is used to...
Write a Java program that asks the user to enter an integer that is used to set a limit that will generate the following four patterns of multiples of five using nested loops •Ascending multiples of five with ascending length triangle •Ascending multiples of five with descending length (inverted) triangle •Descending multiples of five with ascending length triangle •Descending multiples of five with descending length (inverted) triangle Use error checking to keep asking the user for a positive number until...
In Python write a program that asks the user to enter the monthly costs for the...
In Python write a program that asks the user to enter the monthly costs for the following expenses incurred from operating his or her automobile: loan payment, insurance, gas, oil, tires, and maintenance the program should then display the total monthly cost of these expenses, and the total annual cost of these expenses. your program MUST have BOTH a main function AND a function named calcExpenses to calculate the expenses. DO NOT display the expenses inside of the calcExpenses function!!...
Write a program that asks the user to enter an unsigned number and read it. Then...
Write a program that asks the user to enter an unsigned number and read it. Then swap the bits at odd positions with those at even positions and display the resulting number. For example, if the user enters the number 9, which has binary representation of 1001, then bit 0 is swapped with bit 1, and bit 2 is swapped with bit 3, resulting in the binary number 0110. Thus, the program should display 6. COMMENT COMPLETE CODE PLEASE
In a file called LengthSum.java, write a program that: - Asks the user to enter a...
In a file called LengthSum.java, write a program that: - Asks the user to enter a string. - Asks the user to enter a second string. - Prints out the length of the first string, the length of the second string, and the sum of the two lengths, using EXACTLY the same format as shown below. For example: if the user enters strings "UT" and "Arlington", your program output should look EXACTLY like this: Please enter a string: UT Please...
1/Write a C program that asks the user to enter 5 nums and adds each number...
1/Write a C program that asks the user to enter 5 nums and adds each number to a total. The program should then display the total. To add to the total, the program must call the function void sum(int value, int *ptotal) passing the value the user entered and the address of the total (a local variable in main). The function should add the value to the total. Note that the function is void and does not return anything. Don't...
Write a design algorithm and a python program which asks the user for the length of...
Write a design algorithm and a python program which asks the user for the length of the three sides of two triangles. It should compute the perimeter of the two triangles and display it. It should also display which triangle has the greater perimeter. If both have the same perimeter it should display that the perimeters are the same. Formula: Perimeter of triangleA = (side1A + side2A +side3A) See the 2 sample outputs. Enter side1 of TriangleA: 2 Enter side2...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT