0

I am trying to create a program that will allow users to enter a list of names. And then the program will pull from the ArrayList randomly and pull each name one at a time until all names have been used. I have the scanner part completed as seen below:

public class Auction
{
    public static void main (String[] args)
    {
        Scanner scan = new Scanner(System.in);
        ArrayList<String> names = new ArrayList<String> ();
        char quit = 'Y';
        String playername = null;

        while (quit == 'Y')
        {
            System.out.println("\nPlayer Name:");
            playername = scan.next();
            names.add (playername);

            System.out.print("Enter Another Name? (Y/N) \n");
            String word = scan.next();
            word = word.toUpperCase();
            quit = word.charAt(0);
        }
      }
   }

I have another class where I tried to complete the random generation with no success. There doesn't appear to be any errors but it's not working either. I know I am way off on the "random without replacing" part but I was just trying to get it to work before I moved on. I'm not sure if I am even referencing the ArrayList from the other Auction Class. Like a lot of others, I am new to Java so be gentle. I have spent a week on this which should probably have taken me a few hours. I appreciate your help.

public class Draft
{
    Random randomGenerator;
    ArrayList<String> names;

    String randName() {
    int index = randomGenerator.nextInt(names.size());
    System.out.println("Next on the Block" + names.get(index));
    return names.get(index);
    }
}
3
  • what u want to achieve i mean u like to create String from arraylist? Commented Jul 9, 2014 at 5:00
  • @Josh I think its better to instantiate the randomGenerator because the constructor Random() --> Creates a new random number generator. This constructor sets the seed of the random number generator to a value very likely to be distinct from any other invocation of this constructor. Commented Jul 9, 2014 at 9:25
  • I want to take the names that the user enters as the ArrayList and then randomly generate them one at a time without repetition until all have been generated. It's basically a name auction. The list of names go in. Then, say with each keystroke (spacebar) they will randomly be called to bid on, one by one, until all have been bid on. This program just takes the names entered and randomly generates them one at a time so the audience can bid on them. Commented Jul 9, 2014 at 15:48

1 Answer 1

2

Just use Collections.shuffle() to shuffle the list itself:

Collections.shuffle(names);

Your list is now randomized and you can take elements from the top until it's empty. For example, using an iterator:

Iterator<List> it = names.iterator();

while (it.hasNext()) {
    String name = it.next();

    it.remove(); // optionally remove

    System.out.println("Next on the block" + name);
}

Or, if there's no need to actually remove the name from the list, using a simple for loop:

for (String name : names) {
    System.out.println("Next on the block" + name);
}
Sign up to request clarification or add additional context in comments.

4 Comments

I understand what you are explaining. And I understand the concept. But when I try to actually implement this I have a ton of errors. It's almost like it is not recognizing my ArrayList from the Auction class. What should the Draft class code exactly look like in either case with the for or while?
This is not working. I keep getting errors on the "names" in Collection and the Iterator. Like I previously mentioned, it's almost like this class doesn't recognize the ArrayList "names". Which may be my problem. It the call to that ArrayList correct?
@Josh You're just not passing your names object to your Draft class. Create a constructor in Draft: public Draft(List<String> names) { this.names = names; }, create a new Draft object in your main method, and pass in the names to the Draft constructor.
I would love if you could physically show me how it should look in my Draft class. Because it is still showing errors and not noticing the names list. Like I mentioned before, creating the List is working fine. The draft class is where I am having all the errors. I am a "show me" learner. If you show me what you mean by using my code I will learn and absorb it going forward. So please show me what my draft class should look like.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.