Question

read the code and comments, and fix the program by INSERTING the missing code in Java...

read the code and comments, and fix the program by INSERTING the missing code
in Java THank you please

import java.util.Scanner;

public class ExtractNames
{
    // Extract (and print to standard output) the first and last names in "Last, First" (read from standard input).
    public static void main(String[] args)
    {
        // Set up a Scanner object for reading input from the user (keyboard).
        Scanner scan = new Scanner (System.in);
        
        // Read a full name from the user as "Last, First".
        System.out.println ("Enter your full name as Last, First");
        String fullName = scan.nextLine ();
        
        // Find the index of the comma in the String entered by the user,
        // because the comma separates the last name from the first name.
        int positionOfComma =  *** INSERT CALL TO indexOf METHOD HERE ;
        
        // Extract the last and first names based on the index of the comma.
        String lastName = fullName.substring ( *** INSERT PARAMETERS HERE );
        String firstName = fullName.substring ( *** INSERT PARAMETERS HERE );
        
        // Print the first name and last name.
        System.out.println ("First name: " + firstName);
        System.out.println ("Last name: " + lastName);
        
    } // end main
    
} // end ExtractNames
0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class ExtractNames
{
    // Extract (and print to standard output) the first and last names in "Last, First" (read from standard input).
    public static void main(String[] args)
    {
        // Set up a Scanner object for reading input from the user (keyboard).
        Scanner scan = new Scanner (System.in);

        // Read a full name from the user as "Last, First".
        System.out.println ("Enter your full name as Last, First");
        String fullName = scan.nextLine ();

        // Find the index of the comma in the String entered by the user,
        // because the comma separates the last name from the first name.
        int positionOfComma =  fullName.indexOf(',');

        // Extract the last and first names based on the index of the comma.
        String lastName = fullName.substring (0,positionOfComma);
        String firstName = fullName.substring (positionOfComma+2,fullName.length());

        // Print the first name and last name.
        System.out.println ("First name: " + firstName);
        System.out.println ("Last name: " + lastName);

    } // end main

} // end ExtractNames

Add a comment
Know the answer?
Add Answer to:
read the code and comments, and fix the program by INSERTING the missing code in Java...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Exercise 1 • Examine ShoppingCart.java. – Perform the following: – Use the indexOf method to get...

    Exercise 1 • Examine ShoppingCart.java. – Perform the following: – Use the indexOf method to get the index for the space character (" ") within custName. Assign it to spaceIdx. – Use the substring method and spaceIdx to get the first name portion of custName. Assign it to firstName and print firstName. public class ShoppingCart {     public static void main (String[] args){         String custName = "Steve Smith";         String firstName;         int spaceIdx;                 // Get the...

  • Java Homework Problems: 4. • Examine AddImport.java. – Perform the following: – Replace the fully qualified...

    Java Homework Problems: 4. • Examine AddImport.java. – Perform the following: – Replace the fully qualified name to access the Jlabel component with an import statement. – To import classes from the util package, replace multiple import statements with a single import statement. /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ import java.util.Calendar; import java.util.Date; public...

  • JAVA PROGRAMMING In this final review lab from our intro course, use the Name class you...

    JAVA PROGRAMMING In this final review lab from our intro course, use the Name class you created in the previous lab. In this lab, create a Student class with the following class variable: Student name: Name (Note: Name is a datatype) Student Id: String Create the getter and setter methods. In addition to these methods, create a toString() method, which overrides the object class toString() method. This override method prints the current value of any of this class object. Complete...

  • Write a program that reads a person's first and last names, separated by a space. Then...

    Write a program that reads a person's first and last names, separated by a space. Then the program outputs last name, comma, first name. End with newline. Example output if the input is: Maya Jones Jones, Maya import java.util.Scanner; public class SpaceReplace {    public static void main (String [] args) {       Scanner scnr = new Scanner(System.in);       String firstName;       String lastName; //answer goes here//    } }

  • Write a program that reads a person's first and last names, separated by a space. Then...

    Write a program that reads a person's first and last names, separated by a space. Then the program outputs last name, comma, first name. End with newline. Example output if the input is Maya Jones Tones, Maya import java . util·Scanner; public class SpaceReplace 4 public static void main (String [1 ares) f( Scanner scnr-new Scanner(System.in); String firstName; String lastName; Your solution goes here */ 10 12

  • has to be done in c programming not c++ 5:28 . O Done Lab_Seven Lun, wuUULIVIL...

    has to be done in c programming not c++ 5:28 . O Done Lab_Seven Lun, wuUULIVIL - - - - - - - - Write a program that prompts the user to input their first name, from the keyboard and stores them in the variable "firstName". It does the same for last name and stores it in the variable "lastName". It then uses strcat to merge the two names, separates them by a space and stores the full name into...

  • JAVA Can you make this return the first letter of  first and last name capitalized? import java.io.File;...

    JAVA Can you make this return the first letter of  first and last name capitalized? import java.io.File; import java.io.IOException; import java.util.*; public class M1 {    public static void main(String[] args) {        //scanner input from user    Scanner scanner = new Scanner(System.in); // System.out.print("Please enter your full name: "); String fullname = scanner.nextLine(); //creating a for loop to call first and last name separately int i,k=0; String first="",last=""; for(i=0;i<fullname.length();i++) { char j=fullname.charAt(i); if(j==' ') { k=i; break; } first=first+j;...

  • Write a C++ program that produces the following output: /* OUTPUT Enter your age: 21 Enter...

    Write a C++ program that produces the following output: /* OUTPUT Enter your age: 21 Enter the last name: Lee Hello Tom Lee. You are 21 years old. Press any key */   Declare an array named: firstName The array is a c_string, i.e., it is a null-terminated character array. The size of the array is 10. Assign a first name to it when it is declared.   Declare an array named: lastName The array is a c_string, The size of the...

  • Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class...

    Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class should contain the following data fields and methods (note that all data and methods are for objects unless specified as being for the entire class) Data fields: A String object named firstName A String object named middleName A String object name lastName Methods: A Module2 constructor method that accepts no parameters and initializes the data fields from 1) to empty Strings (e.g., firstName =...

  • FOR JAVA: Summary: Create a program that adds students to the class list (see below). The...

    FOR JAVA: Summary: Create a program that adds students to the class list (see below). The solution should be named Roster402_v2.java. Allow the user to control the number of students added to the roster. Ask if the user would like to see their new roster to confirm additions. If yes, then display contents of the file, if no, end the program. ------------------------------------------------------------------------------------- List of student names and IDs for class (this will be your separate text file): Jones, Jim,45 Hicks,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT