Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generating random letters

 
Jessica Egart
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Jessica Egart
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's from any 3 letters in their name...


Jess
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49784
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10504
64
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jessica Egart
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Pie
Posts: 1801
28
Chrome Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Winston Gutkowski
Bartender
Pie
Posts: 10504
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Jessica Egart
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic