How do I create a name generator?

14,264

Solution 1

Seems like homework to me, so I will give a hint. look for the method substring() and charAt() for the first part, and the Random class for the second.

Solution 2

I am a .NET developer so I can't help you with the syntax but you would need to grab the first char of the first name, usually accessible via an indexer - firstName.charAt(0), and a substring of the second one that ranges from the first character (ordinal 0) to the 5th character (ordinal 4), likely something like lastName.substring(0, 4); and concatenate these two strings -

concatenatedName = firstName.charAt(0) + lastName.substring(0, 4);
Share:
14,264
Chickadee
Author by

Chickadee

Updated on June 04, 2022

Comments

  • Chickadee
    Chickadee almost 2 years

    I am creating a program that prompts a first and last name then prints 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.

    I know how to prompt for the name and find the random number but I'm not sure how to

    "print a string composed of the first letter of the first name, followed by the first five letters of the last name."

    Can anyone help me? I am a very elementary Java programmer.

    So I am really close to finishing this but it keeps saying "illegal start of expression" for line 55 and I can't figure it out. Here is my code, sorry, I know it's a mess:

    Random generator = new Random();
    
    int num1;
    
    num1 = generator.nextInt(10-99);
    

    line 55: public String substring; <<<

    String result;
    
    
    System.out.println("Result:" + (beginIndex) + (firstname.substring(0,1) + lastname. substring (0,5)) + (num1) );