I'm new when it comes to Java and programming in general, but I feel I have a decent grasp on a lot of the fundamentals.
I've created this "random planet generator" that is virtually useless aside from minimal entertainment, but I've created it to help solidify what I've been learning. I'm not asking for code, but for a direction with as much info as possible.
My random planet generator lists:
- name:
- temp:
- size (circumference):
- Density:
- Length of Day:
- Number of Moons:
- Gravity:
- If planet has rings:
My problem is, that for the name, it is horribly inefficient. Before I added the name field, I could generate millions of random planets in seconds. Now, I only set it to 50,000 planets and it still takes a minute or two to generate them all.
Here's the method I use to name the planets:
public void setName() throws FileNotFoundException {
String prefix = "";
String greek = "";
int number;
char suffix;
Random rand = new Random();
// randomly pull from list of star names
int randName = rand.nextInt(300);
File prefixFile = new File("StarNames.txt");
Scanner inputFilePrefix = new Scanner(prefixFile);
for (int i = 0; i <= randName; i++) {
prefix = inputFilePrefix.nextLine();
}
inputFilePrefix.close();
// randomly pull of list of Greek names
int randGreek = rand.nextInt(11);
File greekFile = new File("Greek.txt");
Scanner inputFileGreek = new Scanner(greekFile);
for (int i = 0; i <= randGreek; i++) {
greek = inputFileGreek.nextLine();
}
inputFileGreek.close();
// random number
int randNum = rand.nextInt(1000);
number = randNum;
// random letter
int randLetter = rand.nextInt(4);
if (randLetter == 0) {
suffix = 'a';
} else if (randLetter == 1) {
suffix = 'b';
} else if (randLetter == 2) {
suffix = 'c';
} else {
suffix = 'd';
}
// append to class name
this.name = prefix + " " + greek + "-" + number + "" + suffix;
}
As you can tell, I have 2 text files. One for actual star names, and another for (what I think is) the Greek alphabet. It randomly picks a line to read up to and uses that name. What could be a faster, more efficient way of doing this?
The name structure would essentially be something like:
Name: Gemini ALPHA-229c
I have every planet generated added to an ArrayList
one by one after all planets fields are created.
Any suggestions?