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

Add to the code below so that it reads the user\'s first and last name (an entir

ID: 645419 • Letter: A

Question

Add to the code below so that it reads the user's first and last name (an entire line as a single string), then prints the last name followed by a comma and the first initial. (You may assume that the user types valid input consisting of a first name, a single space, then a last name.) For example:

(Bare code: For this problem, you are supposed to just submit a fragment of Java code as described. You shouldnot write any class or method heading around your code; just write the lines of code that will produce the result described.)

Scanner console = new Scanner(System.in);
System.out.print("Type your name: ");

// your code goes here

System.out.println("Your name is: " + name);

Explanation / Answer

import java.util.*;
public class Test {


   public static void main(String[] args) {
       Scanner console = new Scanner(System.in);
       System.out.print("Type your name: ");
       String fullname=console.nextLine();
      
       String s=fullname.substring(fullname.indexOf(' '),fullname.length());
      
       System.out.println("Your name is: " + s+", "+fullname.charAt(0)+".");

   }

}

On Console, the output is:------------------------

Type your name: Jessica Miller
Your name is: Miller, J.

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