Ask Question
23 January, 23:09

4.6.1: Reading and outputting strings. 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

+4
Answers (1)
  1. 24 January, 02:05
    0
    import java. util. Scanner;

    public class num5 {

    public static void main (String[] args) {

    Scanner in = new Scanner (System. in);

    System. out. println ("Enter your full name");

    String name = in. nextLine ();

    String [ ] n = name. split (" ");

    System. out. println (n[0]+", "+n[1]);

    }

    }

    Explanation:

    Scanner class is used to receive the full names. e. g "Maya Jones"

    This is saved in a variable called name using the nextLine () method to grab the entire line of string

    The string split () method is used to split the string on the whitespace

    The output statement is used to print the value at index 0 and index 1 concatenated with a comma.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “4.6.1: Reading and outputting strings. Write a program that reads a person's first and last names, separated by a space. Then the program ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers