Chickadee
Chickadee

Reputation: 109

How do I create a name generator?

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) ); 

Upvotes: 2

Views: 15415

Answers (6)

R1D1CUL3
R1D1CUL3

Reputation: 41

See how easy it is? I gave 4 simple names which can be replaced with words and such. The "4" in the code represents the number of names in the String. This is about as simple as it gets. And for those who want it even shorter(all I did was decrease the spacing):

import java.util.*; 
public class characters{ 
public static void main(String[] args){
Random generate = new Random(); 
String[] name = {"John", "Marcus", "Susan", "Henry"};
System.out.println("Customer: " + name[generate.nextInt(4)]); }}

Upvotes: 0

keuleJ
keuleJ

Reputation: 3486

Take a look at String.substring()

Upvotes: 0

Michael Krussel
Michael Krussel

Reputation: 2656

You're missing a closing parentheses for println.

I would recommend removing all the parentheses around the string concats they just make it hard to read.

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

Also what happens if the user enters a last name with only 4 characters?

Upvotes: 0

Ragnar123
Ragnar123

Reputation: 5204

Something like this will do

import java.lang.String;
import java.io.*;
import java.util.Random;


class Name {
    public static void main(String args[]) {

        Random rnd = new Random(); // Initialize number generator

        String firstname = "Jessica"; // Initialize the strings
        String lastname = "Craig";

        String result; // We'll be building on this string

        // We'll take the first character in the first name
        result = Character.toString(firstname.charAt(0)); // First char

        if (lastname.length() > 5)
            result += lastname.substring(0,5);
        else
            result += lastname; // You did not specify what to do, if the name is shorter than 5 chars

        result += Integer.toString(rnd.nextInt(99));


        System.out.println(result);

    }
}

Upvotes: 0

cchamberlain
cchamberlain

Reputation: 17956

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);

Upvotes: 1

MByD
MByD

Reputation: 137322

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.

Upvotes: 8

Related Questions