aspose file tools*
The moose likes Beginning Java and the fly likes Generating random letters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Generating random letters" Watch "Generating random letters" New topic
Author

Generating random letters

Jessica Egart
Greenhorn

Joined: Mar 14, 2012
Posts: 5
Hi, i'm relatively new to Java programming and have been asked to design a program that generates a "License plate" number.

the three letters of the license plate are generated from the user's input of their first name, and the three numbers are a randomly generated integer between 100 and 999.
I know how to generate the random integer, but am having terrible trouble generating the random letters.

Here's what I have so far:

Any help would be much appreciated!

Thanks,
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

Jessica Egart wrote:
the three letters of the license plate are generated from the user's input of their first name


How are the letters supposed to be generated from the user's first name? Just pick any 3 from their name? or is it the first 3 letters?


Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
Jessica Egart
Greenhorn

Joined: Mar 14, 2012
Posts: 5
it's from any 3 letters in their name...


Jess
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

If you are just picking 3 random letters from the user's first name then there are some things to consider:

1) You need to know the length of the user's first name, store it in some variable
2) You can generate a random number from 0 to the length of the user's first name, this will represent a single character in the user's first name (do this 3 times, once for every letter you need)
3) Take a look at the charAt() or the substring() method in the String class API and see if you can find a way to use the numbers you randomly generated to get the character from the user's first name at the positions you generated in step 2

Try that and post back.

Hope this helps,
Hunter
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
Welcome to the Ranch

What about somebody called Al or Jo? Would you use AAA AAL ALA LAA LAL LLA ALL or LLL for Al?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7072
    
  16

Jessica Egart wrote:I know how to generate the random integer, but am having terrible trouble generating the random letters.

To be honest, they're not quite random; they're a random selection from a predetermined set (the name). My advice would be to look at Collections.shuffle().

Also: you probably need to decide what you'd want to do to generate a license plate for someone like 'U' Thant or Bo Diddley.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Jessica Egart
Greenhorn

Joined: Mar 14, 2012
Posts: 5
Hi all,

Thanks for all your replies...

I just realised i should use the java.math class rather than the java.random class...
However, it still hasn't gotten much further....

We were just told to generate a program that could be used with any name the user inputs, no matter how short or long... Duplicates are fine...

I've been working on it for a while now, and it's doing my head in!


Jess
J. Kevin Robbins
Ranch Hand

Joined: Dec 16, 2010
Posts: 632
    
    7

This must be a late assignment since I see you posted at 12:58 am and you were either back on or still on at 7:43 am. Ouch. I admire your diligence.

Hunter gave you some good tips. What do you have so far?


"There is no reason for any individual to have a computer in his home" ~ Ken Olson, Co-founder of DEC, 1977
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7072
    
  16

Jessica Egart wrote:I just realised i should use the java.math class rather than the java.random class...

Actually, don't. java.util.Random is newer than java.lang.Math, and in fact Math.random() simply runs Random.nextDouble() anyway, so you may as well just run it yourself. Another advantage of using Random is that you can set up predetermined series of values for testing by using its Random(seed) constructor.

Winston
Jessica Egart
Greenhorn

Joined: Mar 14, 2012
Posts: 5
Jk Robbins wrote:This must be a late assignment since I see you posted at 12:58 am and you were either back on or still on at 7:43 am. Ouch. I admire your diligence.


haha... not quite... its not due till Sunday, but i want to try and get it out of the way so I can do some work on my ecomonics...

We actually haven't been taught about the java.random.class, but after re-reading the question I noticed it said "Use either Math.random() or an instance of the Random class from java.util to generate the random numbers you require.", so I guess I can use either?

I've still got the same as above, because whenever I add something new, i test it, and if it doesn't work, delete it... Needless to say, I've deleted about a hundred different things...


Jess
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
Jessica Egart wrote:I've been working on it for a while now, and it's doing my head in!

What problems are you having ? Hunter and Winston have already suggested ways to proceed. Which you choose depends on whether using the same letter more than once is allowed.

Assuming it is (as you have to handle names of one or two letters, it must be) then Hunter's suggestion is the way to go. You know how to generate numbers between 100 and 999, so just alter that to generate numbers between 1 and the length of the name. Do that three times and use the String.charAt method each time to get the letter from the name.

If it's not then I would do it the way Winston suggests, using Collections.shuffle.

Have a go at one of them and if you have problems, post your code with details of what the problems are (compiler errors, the program not doing what you expect).

Joanne
Jessica Egart
Greenhorn

Joined: Mar 14, 2012
Posts: 5
Hey guys,

Thanks for all your help! I figured it out and it's running fine!

in the end, this is what i ended up with:
import java.util.Scanner;

public class Q2 {

/**
* @param args
*/
public static void main(String[] args) {
Scanner enterFirstName= new Scanner (System.in);
String firstName;

System.out.println("Car Registration Number Generator");
System.out.println("");

System.out.println("Please enter your first name:");
firstName= enterFirstName.next();

int nameLength= firstName.length();
int letterOneNumber= 0+ (int) (Math.random()*nameLength);
int letterTwoNumber= 0+ (int) (Math.random()*nameLength);
int letterThreeNumber= 0+ (int) (Math.random()*nameLength);

char letterOne= firstName.charAt(letterOneNumber);
char letterTwo= firstName.charAt(letterTwoNumber);
char letterThree= firstName.charAt(letterThreeNumber);

String letterOneRego= Character.toString(letterOne);
String letterTwoRego= Character.toString(letterTwo);
String letterThreeRego= Character.toString(letterThree);

String letterOneRegistration= letterOneRego.toUpperCase();
String letterTwoRegistration= letterTwoRego.toUpperCase();
String letterThreeRegistration= letterThreeRego.toUpperCase();

String registrationLetters= letterOneRegistration + letterTwoRegistration + letterThreeRegistration;
int licensePlateNumber = 100 + (int) (Math.random()*999);

System.out.println("Your registration number is: " + registrationLetters + " " + licensePlateNumber);

Thanks again for all your help!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generating random letters
 
Similar Threads
Assignment help
Variables assigned to objects
no main method found
problems with math methods
problem with if statement