107k views
0 votes
Write a program that does the following:

1. Declare the String variables firstname and lastname.
2. Prompt the user for first name and last name.
3. Read in the first name and last name entered by the user.
4. Print out Hello follow by user

1 Answer

1 vote

Answer:

Following is the program in Java language:

import java.util.*;//import package

public class Main // main class

{

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

{

String firstname,lastname; // Declare the two String variables

Scanner ob=new Scanner(System.in); // create a object of scanner //class

System.out.println("Enter the first name:"); // Prompt the user for // enter the first name

firstname=ob.nextLine();// Read in the first name by user

System.out.println("Enter the last name:"); // Prompt the user for last //name

lastname=ob.nextLine();// Read in the last name by user

System.out.println("Hello " + firstname +' ' +lastname); // print the //firstname,lastname

}

}

Output:

Enter the first name:

San

Enter the last name:

ert

Hello San ert

Step-by-step explanation:

Following are the description of program

  • Declared two variable of string type i.e "firstname" and "lastname".
  • Create a instance or object of scanner class .i.e "ob".
  • Prompt the user to enter the first name in the "firstname" variable
  • Read in the first name by the user by using the method nextLine() in the first name variable
  • Prompt the user to enter the last name.
  • Read in the last name by the user by using the method nextLine() in the lastname variable.
  • Finally, print the first name and last name.

User Nicolas De Loof
by
3.8k points