Write a program that prompts for and reads the users first a

Write a program that prompts for and reads the user\'s first and last name (separately). Then print a string composed of the first letter of the, user\'s first name, followed by the first three characters of the user\'s last name.

Solution

FirstnameLastname.java

import java.util.Scanner;

public class FirstnameLastname {

   public static void main(String[] args) {
      
       //Declaring variables
       String fname,lname,res;
      
       //Scanner object is used to get the inputs entered by the user
       Scanner sc=new Scanner(System.in);

       //Getting the firstname entered by the user
       System.out.print(\"Enter the firstname :\");
       fname=sc.next();
      
       //Getting the lastname entered by the user
       System.out.print(\"Enter the lastname :\");
       lname=sc.next();
      
       /* Concatenating the first character of firstname
       * and first three characters of lastname
       */
       res=fname.charAt(0)+lname.substring(0,3);
      
       //Displaying the result
       System.out.println(\"The result is :\"+res);
      
   }

}

_________________

Output:

Enter the firstname :Kane
Enter the lastname :Williams
The result is :KWil

_____________Thank You

 Write a program that prompts for and reads the user\'s first and last name (separately). Then print a string composed of the first letter of the, user\'s first

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site