Random username generator with random number behind errors
I have been on this for about an hour and I really can't figure this out,
I am suppose to make something 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 five characters of
the user's last name, followed by a random number in the range 10 to 99.
and assume that the last name is at least five letters long
import java.util.Scanner; //Needed for the Scanner class
import java.lang.String;
import java.util.Random;
public class UsernameGenerator
{
public static void main(String[] args) //all the action happens here!
{ Scanner input = new Scanner (System.in);
Random generator = new Random();
int num1;
num1 = generator.nextInt(10-99);
String firstName;
String lastName;
String concatenatedName;
System.out.println("Enter your First Name: ");
firstName = input.next();
System.out.print( "Enter your Last Name: " );
lastName = input.next();
// We'll take the first character in the first name
concatenatedName = firstName.charAt(0) + lastName.substring(0, 5) + num1;
Random rnd = new Random(); // Initialize number generator
if (lastName.length() > 5)
concatenatedName += lastName.substring(0,5);
else
concatenatedName += lastName; // You did not specify what to do,
if the name is shorter than 5 chars
concatenatedName += Integer.toString(rnd.nextInt(99));
System.out.println();
}
}
No comments:
Post a Comment