Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

7.8 Lab: Using String Methods (1) Read in a phone number is the following format

ID: 3703165 • Letter: 7

Question

7.8 Lab: Using String Methods

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

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.

(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.

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

(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:

Sample output:

(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:

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. */
}

}

Explanation / Answer

FormatStrings.java

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. */
}

}

Output:

Enter the phone number :
111-222-3333
First three digit are : (111)
Last digits are : 222-3333
New phone number string is : (111)222-3333

Please enter the address :
Santa Fe, New Mexico
Output is upper case is : SANTA FE, NEW MEXICO
New address string is : NEW MEXICO:SANTA FE

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote