1

I have run into a little problem. I have a homework exercise where i need to 'randomly' select a string from a string array. The goal of the exercise is to make a code which choses a random (inserted) name. My code:

 public void run() {
        int userSelection = -1;
        int userAmount = 0;
        String[] users = new String[userAmount];
        int[] amountChosen = new int[userAmount];

        while (userSelection != 0) {
            drawMenu();
            System.out.println();

            //user selecting the menu choice
            System.out.print("Make a selection from the menu: ");
            userSelection = userInput();
            System.out.println();

            //forcing the user to give one of the allowed values
            while (userSelection < 0 || userSelection > 4) {
                System.out.print("That is invalid input. try again: ");
                userSelection = userInput();
            }

            //adding users
            if (userSelection == 1) {
                System.out.print("How many users do we have?");
                userAmount = userInput();
                users = new String[userAmount];
                amountChosen = new int[userAmount];
                addUsers(users, userAmount); //returns user array with names
                System.out.println();
            }

            //selecting random user
             else if (userSelection == 2) {
                int playerSelect = (int) (Math.random()*userAmount);
                amountChosen[playerSelect]++;
                System.out.println(users[playerSelect] + " was chosen!");
                System.out.println();
            }

            //display the amount the users were chosen
            else{
                System.out.println("******** Turns ********");
                for (int i = 0; i < userAmount; i++){
                    System.out.println("* " + "[" + amountChosen[i] + "] " + users[i]);
                }
                System.out.println("***********************");
                System.out.println();
            }
        }
    }

As you can see i now have a totally random userselection. For keeping tabs on how often a player is chosen i already made the 'int[] amountChosen' array. The goal is to "select a random player, also make it chose the player that is chosen the fewest times" so basicly it needs to select the string with the lowest amountChosen. (Also: I am aware my code may be a little bit messy and weird in some places. I just started learning java) Thank you for response!

1
  • Choosing a random player is not choosing a set player or lowest chosen player. Now you can do a random set if there is more than 1 player that has been choose the lowest amount of times. Is this what you are trying to accomplish? Commented Oct 11, 2018 at 21:12

2 Answers 2

4

I won't give out the answer to your assignment. But, here is a naive implementation of what you are trying to achieve:

private void someMethod() {
    String[] strArray = {"foo", "bar", "foobar"};
    Random random = new Random();
    System.out.println(strArray[random.nextInt(strArray.length)]);
}

Explanation:

You take a random number between 0 and the length of your string array using Random and then just use this as an index to query your string array.

Sign up to request clarification or add additional context in comments.

Comments

0

If I understand correctly, you want to pick randomly one of all players having the lowest amount chosen?

  • Use a Map<String, Integer> with the amounts;
  • Filter the map with all players with the lowest amount;
  • Use Random.nextInt(amountOfLowest).

You could use Java Streams.

Hints: • use map.entrySet().stream() to stream over the map elements • use values(), unboxed() and min() to get the lowest value • use filter(), map() and collect() to collect a list of all players with the lowest amount • use List.get(new Random().nextInt(...)) to select a player.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.