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

I have to add to the main method of this code: import java.util.Scanner; public

ID: 3567374 • Letter: I

Question

I have to add to the main method of this code:

import java.util.Scanner;

public class NameParserApp
{
public static void main(String[] args)
{
  Scanner sc = new Scanner(System.in);
  
  System.out.println("Welcome To The Name Parser. ");
  System.out.println("Enter A Name: ");
  
}
}

In order to produce this:

Exercise 12-3 Parse a name In this exercise, write an application that parses full names into first and last name or first. middle, and last name. depending on whether the user enters a string consisting of two or three words. The output for the program should look something like this: 1. Open the NameParserApp class in the ch 1 2 NameParser directory. 2. Add code to the main method that lets the user enter a full name as a string. Then. add code that separates the name into two or three strings depending on whether the user entered a name with two words or three. Finally. display each word of the name on a separate line. If the user enters fewer than two words or more than three words, display an error message. Also. make sure the application works even if the user enters one or more spaces before or after the name. 3. Compile the program and run it to make sure it works.

Explanation / Answer

import java.util.Scanner;

public class NameParserApp {
   public static void main(String[] args) {
      
       String name;
       Scanner sc = new Scanner(System.in);

       System.out.println("Welcome To The Name Parser. ");
       System.out.print("Enter A Name: ");
       name = sc.nextLine();
      
       String[] names= name.split("\s+");
  
       if(names.length<2 || names.length>3)
       {
           System.out.println("Name must be haivng two or three words");
       } else {
          
           for(int i=0;i<names.length;i++) {
               if(i==0) {
                   System.out.println("First name: "+names[i]);
               } else if(i==1){
                   System.out.println("Last name: "+names[i]);
               } else if(i==2) {
                   System.out.println("Middle name: "+names[i]);

               }
           }
       }
   }
}