-6

So I have to get the users first and last name which I have but then I have to take the first letter of the persons first name and concatenate it to the last name to create a username (all lowercase)

but I'm stuck on how to add that... Im assuming I have to use one of the methods from the string class but I'm not exactly sure which one or how to do it?

 package userinput.java;
 import java.util.Scanner;

 public class UserinputJava {


 public static void main(String[] args) 
  {
   Scanner userInput = new Scanner (System.in);

   String firstname;
   String lastname;
   String fullname;

   System.out.print("Enter first name: ");
   firstname = userInput.next();

   System.out.print("Enter last name: ");
   lastname = userInput.next();


   fullname = firstname + lastname;
   System.out.println ("You are " + fullname + "!");




  }

 }
marie
  • 1
  • 1
  • 4

2 Answers2

3
String fullname = firstname.charAt(0) + lastname;

Better yet, use a StringBuilder.

EDIT: Actually it's not better because the compiler converts the above line to the line below automatically. The above line is more readable.

String fullname = new StringBuilder().append(firstname.charAt(0)).append(lastname).toString();
Anubian Noob
  • 13,426
  • 6
  • 53
  • 75
1

You can use the charAt method to extract the first (or any other) character of a string. Note that charAt returns a char primitive, not a String, so it's more convenient to use a StringBuilder:

StringBuilder sb = new StringBuilder (lastname.length() + 1);
sb.append(firstname.charAt(0));
sb.append(lastname);
String userName = sb.toString().toLowerCase();
Mureinik
  • 297,002
  • 52
  • 306
  • 350