In: Computer Science
Java Programming :
Email username generator Write an application that asks the user to enter first name and last name. Generate the username from the first five letters of the last name, followed by the first two letters of the first name. Use the .toLowerCase() method to insure all strings are lower case. String aString = “Abcd” aString.toLowerCase(); aString = abcd Use aString.substring(start position, end position + 1) aString.substring(0, 3) yields the first 3 letters of a string If the last name is no more than five letters, use the entire name. If it is more than five letters, use the first 5 letters Print the email username you generated with @myCollege.edu appended
import java.util.*;
class emailGenerater
{
public static void main(String[] args)
{
// using scanner object for input.
//System.in is a standard input stream
Scanner sc = new Scanner(System.in);
System.out.println("First name :");
//reads firstName
String firstName = sc.next();
System.out.println("Last Name :");
// Reading Last Name
String lastName = sc.next();
// We have to check the length of the last name.
// using another variable so that original name variable does't change.
String newlast = lastName;
int len = newlast.length();
if(len >5)
{
newlast = newlast.substring(len-5,len);
}
// first 2 letter from first name.
// we can also make a condition for first name like lastname in above
// but there is not mention any condition on the first name.
String newfirst = firstName.substring(0,2);
// concetnate last and first substrings.
String username = newlast+newfirst;
// convert username in lower case.
username = username.toLowerCase();
// appending given format in the username.
String Email = username + "@myCollege.edu";
// Now print the email
System.out.println(Email);
}
}
Above is the code form email generater.