CHALLENGE ACTIVITY 2.15.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.

Respuesta :

Answer:

Following are the program in Java language

import java.util.*; // import package

public class Main  // main function

{

public static void main(String[] args)  // main class

{

    String l_name,f_name; // variable declaration

    Scanner sc6=new Scanner(System.in); // scanner class

   f_name= sc6.next(); // Read input

 l_name=sc6.next(); // Read input

 System.out.print(l_name); // display last name

  System.out.print(","); // display comma

   System.out.println(f_name); // display first name

}

}

Output:

Maya Jones

Jones, Maya

Explanation:

Following are the description of program

  • Declared a variable  'l_name","f_name" as the string type .
  • Create the instance of scanner class i.e "sc6".
  • Read the user input by using the instance "sc6" of the scanner class in the variable "l_name" and "f_name ".
  • Finally by system.print() method we will display the value according to the format in the given question .