Question: 7.8 Lab: Using String Methods (1) Read in a phone number is the following format (no prompting necessary). Sample input: 111-222-3333 Use a String method

7.8 Lab: Using String Methods

(1) Read in a phone number is the following format (no prompting necessary). Sample input:

111-222-3333 

Use a String method to save the area code in a new String. For example: 111

Modify the new String by adding parentheses around the area code. For example: (111)

Then display the new String, followed by a newline.

(111) 

(2) Use a String method to remove the first four characters, through the first dash, from the original String. Then display the revised original String, followed by a newline.

222-3333 

(3) Pre-pend the new String to the original String. Then display the resulting String, followed by a newline.

(111)222-3333 

(4) Read in a city and state, all at once, separated by a comma (no prompting necessary). Note that either one or both may have spaces in the name. Use a String method to uppercase the entire String. Then display the resulting String, followed by a newline. Sample input:

Santa Fe, New Mexico 

Sample output:

SANTA FE, NEW MEXICO 

(5) Use a String method to find the location of the comma. Use a String method to extract the city and store it into a new String. Use a String method to extract the state and store it into a new String. Display the state and city in reverse, using a colon and space to separate them, followed by a newline. Sample output:

NEW MEXICO: SANTA FE

import java.util.Scanner;

public class FormatStrings {

public static void main(String[] args) { //scanner to read the input

Scanner sc = new Scanner(System.in);

String phoneNumber="";

//ask for phone number

System.out.println("Enter the phone number : ");

phoneNumber =sc.nextLine();

//split the number by '-'

//get first string by getting 0 element from array

String firstThreeDigit = phoneNumber.split("-")[0];

firstThreeDigit = "("+firstThreeDigit+")";

System.out.println("First three digit are : "+firstThreeDigit);

//now combined element 1 and 2 by - and store in lastDigits

String lastDigits = phoneNumber.split("-")[1]+"-"+phoneNumber.split("-")[2];

System.out.println("Last digits are : "+lastDigits);

String newNumber = firstThreeDigit+lastDigits;

System.out.println("New phone number string is : "+newNumber);

//address part start

System.out.println(" Please enter the address : ");

String address = sc.nextLine();

//convert to upper case

address = address.toUpperCase();

System.out.println("Output is upper case is : "+address);

String addressSplit[] = address.split(",");

String state = addressSplit[1];

String city = addressSplit[0];

address = state+":"+city;

System.out.println("New address string is :"+address);

sc.close();/* Type your code here. */ }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!