SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Stuart Reges, Marty Stepp
Chapter:
Introduction To Parameters And Objects
Exercise:
Exercises
Question:15 | ISBN:9780136091813 | Edition: 2

Question

Write a method called processName that accepts a Scanner for the console as a parameter and prompts the user to enter a full name, then prints the name in reverse order (i.e., last name, first name). Here is an example dialogue with the user:
Please enter your full name: Sammy Jankis
Your name in reverse order is Jankis, Sammy

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program:

import java.util.Scanner;
public class Ch03Ex15
{
	public static void main(String[] args)
	{
		Scanner keyboard = new Scanner(System.in);

		processName(keyboard);
	}

	public static void processName(Scanner keyboard)
	{
		System.out.print("Please enter your full name: ");
		String fname = keyboard.next();
		String lname = keyboard.next();
		System.out.print("Your name in reverse order is " + lname + ", " + fname);
	}
}

Output:

Please enter your full name: Sammy Jankis
Your name in reverse order is Jankis, Sammy

 

0 0

Discussions

Post the discussion to improve the above solution.