235k views
5 votes
Write a method in java that takes a Scanner object as parameter. The method gets two inputs from the user (the first name and the last name) and prints out the full name of the user.

Your method should use two prompts to ask the user to enter the first name and the last name. Test your method with your first name and last name.

User Anaval
by
7.2k points

1 Answer

4 votes

Final answer:

The required Java method takes a Scanner object, prompts the user for their first and last names, and prints out the full name using two separate prompts.

Step-by-step explanation:

To write a method in Java that takes a Scanner object as a parameter accepts two inputs (first name and last name), and prints out the full name, you would do something like this:

import java.util.Scanner;

public class FullNamePrinter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
printFullName(scanner);
}

public static void printFullName(Scanner scanner) {
System.out.print("Enter your first name: ");
String firstName = scanner.nextLine();
System.out.print("Enter your last name: ");
String lastName = scanner.nextLine();
System.out.println("Your full name is " + firstName + " " + lastName);
}
}

When executed, this code prompts the user twice, first for their first name and then for their last name, before concatenating and printing the full name to the console.

User Entropid
by
7.3k points