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

JAVA Accept user input of a name string. Verify that the name string contains a

ID: 3775104 • Letter: J

Question

JAVA

Accept user input of a name string. Verify that the name string contains a first name and a last name and that both contain valid characters and are from 2 to 20 characters in length. The name string may optionally contain a middle name or middle initial. The name string may also y contain a leading salutation or title (Mr., Mrs., Miss, Dr.).

Also option to add (junior,senior,ll,..)

Determine whether a valid name string has been entered. If not, indicate why. In any case, the program should continue to ask the user for a name string until the user enters "end".

Explanation / Answer

import java.util.*; //import package for scanner class
public class namecheck //class begins   
{

     public static void main(String []args)   //main function begins    
   {
           String[] title= {"Mr.", "Mrs.", "Miss","Dr."}; //array to hold titles
          
           Scanner sc = new Scanner(System.in);       //scanner object
           while(true)                   //loop for servertime prompt name
           {
                 int flag=1;               //flag =1 means assume correct name
           System.out.println("Enter Name in Format : (Mr., Mrs., Miss, Dr.) FirstName MiddleName LastName "); //prompt name
           String name=sc.nextLine();           //read name
           StringTokenizer st = new StringTokenizer(name," ");   //divide name into tokens with white space
           int count = st.countTokens();               //token count
           //   System.out.println("Total tokens : " + st.countTokens());
           if (count<=3)
           {
               System.out.println(" In correct in name.Try again ");
               continue;
           }
           for(int i=0;st.hasMoreTokens();i++)       //loop for check each part of name
           {
                        String stemp=st.nextToken();
                   // System.out.println(stemp);
                   if(i==0)               //check for name title
                        if(!(Arrays.asList(title).contains(stemp))) //check for name title
                        {
                            flag=0;
                       System.out.println("Title In correct in name");
                        }
                  
                   if(i==1)               //check for first name
                        if(!(stemp.length()>=2 && stemp.length()<=20 )) //check for length of first name
                        {
                            flag=0;
                       System.out.println("First name In correct in name");
                        }

                       //check for last name & length
                   if(count==4)   //count 4 means name have middle name otherwise it middle name optional
                   {
                   if(i==3)
                        if(!(stemp.length()>=2 && stemp.length()<=20 ))
                        {
                            flag=0;
                            System.out.println("Last name In correct in name");
                        }
                      
                   }
                   else
                   {
                        if(i==2)
                        if(!(stemp.length()>=2 && stemp.length()<=20 ))
                        {
                             flag=0;
                            System.out.println("Last name In correct in name ");
                        }
                      
                   }
           }
          
                if(flag==0)       //if flag set to 0 means incorrect name
               System.out.println(" In correct in name.Try again ");
               else
                    System.out.println(" Correct in name.Enter another name to check ");
           }  
          
          
      
     }
}


Output :

Enter Name in Format : (Mr., Mrs., Miss, Dr.) FirstName MiddleName LastName                                                                                              

                                                                                                                                                                         

Mr sdf sdflds sdf                                                                                                                                                        

Title In correct in name                                                                                                                                                 

In correct in name.Try again                                                                                                                                            

                                                                                                                                                                         

Enter Name in Format : (Mr., Mrs., Miss, Dr.) FirstName MiddleName LastName                                                                                              

                                                                                                                                                                         

Mr. adfs sdfds sdf                                                                                                                                                       

Correct in name.Enter another name to check