Skip to content
Advertisement

(Java) How can I make a loop that will assign random names I’ve generated from an array to multiple strings?

Relatively new to Java, looking for a solution to a crucial part of a game I’m making for a class. The idea is to make a very simple stock market simulation game, but the problem is related to creating made-up company names. I have three arrays for the first, middle, and last names of companies. I’m trying to make some of these companies have one word names, others two, etc. So far I’ve used a random number generator and if/elif/else statement to simulate one, but I would like five of them and I would prefer a more efficient method of doing so. Here’s the code:

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

class Main {

    public static void main(String[] args) {
        // The code all goes in here

        String[] companyFirstNames = {"Alpine", "Bear", "Bull", "Cuckoo", "Delta", "Dragon", "Echo", "Fighter", "Giant", "H20", "Indo", "Jared", "Jason", "Kicker", "Lodge", "Little", "Manzo", "Mint", "Neighbour", "Nelson", "Ossuary", "Open", "Private", "Poor", "Quick", "Quiant", "Reach", "Rebel", "Space", "Spear", "Titus", "Trebble", "Underdog", "Upper", "Vital", "Vert", "White", "Whistle", "X's", "Yuri's", "Yogurt", "Zebra"};

        String[] companySecondNames = {" Science", " Technology", " Arts", " Research", " Laboratory", " Lodging", " Woodworking", " Fashion", " Oil", " Trading", "  Investing"};

        String[] companyLastNames = {" Limited", " Co.", " Corp.", " Corporation", " Ltd", " Institute", " Association", " Federation", " Firm"};
        //Three arrays of random company name "pieces"

        Random randomNamer = new Random();
        //Used for getting random names & ints

        int randomOne = randomNamer.nextInt(companyFirstNames.length);
        int randomTwo = randomNamer.nextInt(companySecondNames.length);
        int randomThree = randomNamer.nextInt(companyLastNames.length);
        //Three ints that are given random values associated to the arrays

        int numberOne = randomNamer.nextInt(100);
        //Getting a random 0-100 number

        String bigNameCompany = companyFirstNames[randomOne] + companySecondNames[randomTwo] + companyLastNames[randomThree]; 
        String midNameCompany = companyFirstNames[randomOne] + companyLastNames[randomThree];
        String smallNameCompany = companyFirstNames[randomOne];
        //The three types of company names possible to produce

        String companyOne;
        String companyTwo;
        String companyThree;
        String companyFour;
        String companyFive;
        //The five companies I want to name

        if (numberOne <= 45) {
            companyOne = bigNameCompany;
            //The first company name is random and big
        } else if (numberOne <= 85) {
            companyOne = midNameCompany;
            //Two word name
        } else {
            companyOne = smallNameCompany;
            //One word name
        }

        System.out.println(companyOne);
        //printing the name of the first company

        //Can i get a loop to do this more efficiently for 5 companies?

    }

}

Advertisement

Answer

I’d encourage you to play around with it. That’s the fun of programming is solving little puzzles like this, and there are a bunch of ways to do something like this. Here is one to give you some ideas:

    Random randomNamer = new Random();
    String[] companyFirstNames = { "Alpine", "Bear", "Bull", "Cuckoo", "Delta", "Dragon", "Echo", "Fighter", "Giant", "H20", "Indo", "Jared", "Jason", "Kicker", "Lodge", "Little", "Manzo", "Mint", "Neighbour", "Nelson", "Ossuary", "Open", "Private", "Poor", "Quick", "Quiant", "Reach", "Rebel", "Space", "Spear", "Titus", "Trebble", "Underdog", "Upper", "Vital", "Vert", "White", "Whistle", "X's", "Yuri's", "Yogurt", "Zebra" };
    String[] companySecondNames = { " Science", " Technology", " Arts", " Research", " Laboratory", " Lodging", " Woodworking", " Fashion", " Oil", " Trading", "  Investing" };
    String[] companyLastNames = { " Limited", " Co.", " Corp.", " Corporation", " Ltd", " Institute", " Association", " Federation", " Firm" };

    for (int i = 0; i < 5; i++) {
        int chance = randomNamer.nextInt(100);
        int firstIdx = randomNamer.nextInt(companyFirstNames.length);
        int secondIdx = randomNamer.nextInt(companySecondNames.length);
        int lastIdx = randomNamer.nextInt(companyLastNames.length);

        String name = null;
        if (chance <= 45) {
            name = companyFirstNames[firstIdx] + companySecondNames[secondIdx] + companyLastNames[lastIdx];
        } else if (chance <= 85) {
            name = companyFirstNames[firstIdx] + companyLastNames[lastIdx];
        } else {
            name = companyFirstNames[firstIdx];
        }
        System.out.println(name);
    }
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement