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.

Recommended Answers

All 5 Replies

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 ( :

commented: Thank you this helped a lot. I didn't even think about boolean. +1

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.

That turns out to be quite a nice solution great idea.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.