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

Core Lab 2.11 [4 lab points] Using the string for this Co methods in the class L

ID: 3725192 • Letter: C

Question

Core Lab 2.11 [4 lab points] Using the string for this Co methods in the class LabCh025tringMethods, here are the specifications ore Lab. You will also need to declare some integer and String variables to hold various pieces of information. Think carefully what you do in what order to prevent redoing certain statements. 1. From the keyboard, prompt for and type in a name in last name, first name order, with a comma after the last name and a space after the comma. Example: Doe, Jane. Your program can assume a user will always type a name with all the correct information included. Well learn later how to catch exceptions to that rule. (To see what happens when there are errors, enter a name without a comma, and enter the empty string by pressing enter at the prompt.) 2. Print the name in first name and last name order. Example: Jane Doe You have to figure out what methods to use. 3. Print how long (how many characters) the first and last names are with a message identi- fying each part of the name and length. Example: with "Jane Doe" as the name, this step will produce the output: Jane: 4 characters, Doe: 3 characters 4. From the keyboard, type in a phone number in this format: (303) 123-4567. Extract the area code and print it. Your program can assume a user will always type a phone number

Explanation / Answer

LabCh02StringMethods.java

import java.util.Scanner;

public class LabCh02StringMethods {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.println("Enter name: ");

String name = scan.nextLine();

String names[] = name.split(",");

String lastName = names[0].trim();

String firstName = names[1].trim();

System.out.println(firstName+" "+lastName);

int firstNameLength = firstName.length();

int lastNameLength = lastName.length();

System.out.println(firstName+": "+firstNameLength+" characters, "+lastName+": "+lastNameLength+" characters");

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

String phoneNumber = scan.nextLine();

String areaCode = phoneNumber.substring(1,4);

System.out.println("Area code: "+areaCode);

}

}

Output:

Enter name:
Doe, Jane
Jane Doe
Jane: 4 characters, Doe: 3 characters
Enter the phone number:
(303) 123-4567
Area code: 303

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