1

I'm trying to get a random object from an arraylist. The random should be among the object in the arraylist having the variable available as true.

Right now it get a random attendant from the whole arraylist. I just need it to specify a limit to attendants being true(variable) this is the line:

return myAtt.get(randAtt.nextInt(myAtt.size()));

This is the method:

public static Attendant  askForAtt() {
        Scanner scanAtt = new Scanner(System.in);
        Random randAtt = new Random();
        //Attendant  asnAtt = null;
        System.out.println("Do you require an Attendant ? Y or N");
        String response = scanAtt.next();
        if ((response.equals("y")) || (response.equals("yes")) || (response.equals("Yes")) || (response.equals("Y"))) {
            // Cars.setAssignedTo(myAtt.get(randAtt.nextInt(myAtt.size())));
            return myAtt.get(randAtt.nextInt(myAtt.size()));

        } else if ((response.equals("n")) || (response.equals("no")) || (response.equals("No")) || (response.equals("N"))) {
            return new Attendant ("User");
        }
        return new Attendant ("User");    //If input is neither Yes nor No then return new Attendant    
    }

What am I supposed to type? My attendants are like that:

public Attendant(int staffNum, String id, boolean available, attNm name, Cars assign) {
        this.staffNum = staffNum;
        this.id = id;
        this.available = available;
        this.name = name;
        this.assign = assign;
    }

PS:Sorry for my English. It's my 3rd language

5
  • what do you mean by this specify a limit to attendants being true(variable) Commented Sep 21, 2016 at 9:45
  • My attendants have a boolean paremeter I need the ones which are true. Some in the arraylist are false Commented Sep 21, 2016 at 9:48
  • 1
    check the available field for drawed attendand, if its false, draw another (while loop) Commented Sep 21, 2016 at 9:50
  • As a side not, you've duplicated code at the end, you don't need the else if. However, it might be better to keep asking until the solution is eitheir yes or no. Plus, you can use "equalsIgnoreCase" rather than comparing with multiple "no"/"No"/"n"/"N" and yesses. Finally, if you list does not contains any available attends, it will loop infinetely if nothing can change an attendant status outside this loop (if it's not multihreaded) Commented Sep 21, 2016 at 9:53
  • Is it acceptable for you to alter the order of myAtt ? Commented Sep 21, 2016 at 10:34

1 Answer 1

4

On your Attendant class, add this function

public boolean isAvailable(){
   return available;
}

Then

public static Attendant  askForAtt() {

    ...

    if ((response.equals("y")) || (response.equals("yes")) || (response.equals("Yes")) || (response.equals("Y")) && someoneIsAvailable()) {

        ArrayList<Attendant> temp = getAvailableAttendants();
        Attendant attendant = temp.get(randAtt.nextInt(temp.size()));
        return attendant;

    } 

   ...   
}

public boolean someoneIsAvailable(){
    for(int i = 0; i<myAtt.size(); i++){
        if(myAtt.get(i).isAvailable()){
           return true;
        }
    }
    return false;
}

public ArrayList<Attendant> getAvailableAttendants(){
    ArrayList<Attendant> availableAtt = new ArrayList<>();
    for(int i = 0; i<myAtt.size(); i++){
        Attendant att = myAtt.get(i)
        if(att.isAvailable()){
           availableAtt.add(att);
        }
    }
    return availableAtt;
}

Also, you can use

String.equalsIgnoreCase(String);

cause in your trapping the user could do "yEs". Hope that helps. Tell me if something is wrong

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

4 Comments

You could use a dowhile.
This will not work. It does not return the available attendant and could potentially loop infinitely
@brandall Yes you are right about the potential infinite loop, when all attendants were unavailable. So i edited the code and add a condition on the if. But i dont get the This will not work side. Please explain why you think so.
@Android.K.Doe Will not work was perhaps overstated - It would be better for someoneIsAvailable to originally return a list of the available attendants - if it's empty, return - otherwise simply pick a random one at that point - otherwise, you're looping over a list trying at random to get one that is available. If there's only one available in 10,000+, it will take a while to randomly hit it. Down-vote removed.

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.