So I have an array

String[] directions = {"n", "e", "s", "w"};       
        Random dirPick = new Random();
        int rNum = dirPick.nextInt(4);
        String direction = directions[rNum];

Now say that one of these doesn't exist and if it picks it I need to take it out of being able to be picked. so

pick's n

n = full cannot go n

so it creates another random number between 0-3

say it pick n again I want it to know it already tried it and not use it and just create another random number.

then if it tries all 4 (0-3) and they are all full I have a method already that will deal with that.

I've been sitting here thinking of how I could implement this and I'm having a block lol.

Thanks sorry it's long and if you have any questions I'll be checking and get back to you as quick as possible.

by the way this is a very small method in the big picture to my program so I know it doesn't make since to you, but it does.

5 Years
Discussion Span
Last Post by DarkLightning7

you could do it like this:

boolean allChosen = false;
while(allChosen == false){

boolean[] chosen = new boolean[4];
//your code
chosen[rNum] = true

if(chosen[0] == true && chosen[1] == true && chosen[2] == true && chosen[3] == true)
    allChosen = true;

that should do it.

Hope it helps ( :

Edited by DarkLightning7: n/a

Thank you this helped a lot. I didn't even think about boolean.

I maybe wrong but the problem sounds like as if the questioner wants to shuffle the array. If that is the case, Collections.shuffle(Arrays.asList(s)) , I reckon, could be easier. Not that the above code won't work. Just thought I'd add this in in case this may be of any help. Cheers.


In my case your solution was better help as I am not using ArrayList lib. It is a fundamentals class so we are encouraged to try not to use libs. Thus, I'm not using an array list, which would prove to be much easier. Thanks to both of you. Hope


Please mark this thread solved if your questions have been answered. Have a great time learning Java.

Edited by DarkLightning7: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.