Hello guys i've been learning java for a short while and have come upon a problem which i cannot seem to solve.

I have a boolean array of 11 values, now i want to prompt the user into entering the number 1 or 2. If the user enters 1 I want to select ONE random value between 1-5 from my boolean array and give that value to the user. If user chooses 2 then he gets a random value between 6-10. Once a value has been given away once it cannot resurface again.

Any help would be appreciated.

Funny, I was just reading Jon Bentley's article about Tony Hoare's algorithm for generating N random values from 1 to M. It's in the second volume of "Programming Pearls", which has some really good stuff if you're willing to read about old-school stuff. (Most of his examples are written in awk)

Anyway, you're not doing Hoare's method. Okay, first question: are we to assume that the values generated correspond to the index of the array? That is, you're getting a random number in the range [1..5] or [6..10]? (I assume that's what you mean, since you're talking about a boolean array)

Sketch out the procedure, without using any java.
We start with an array of 11 booleans, numbered from 0-10.
We get a number from the user, which is 1 or 2.

Then what?

EDIT: yes the values generated correspond to the index of the array e.g boolean voucher = new boolean[11];

ok jon here it is:
1)we start with an array of 11 booleans, 0-10 however i will use a for statement to start at 1
2)we get a number from the user which is either 1 or 2
3)if user chooses 1 then we get a random number between 1-5 from our boolean array
4)else if user chooses 2 then we get a random number between 6-10 from our boolean array
5)once a number has been given to a customer it can not be used again.

i hope this has made it more clear, the part in which i am struggling is point 3) to point 5).

That's better - I hope it's making it more clear for you, as well.


Here's a few hints for parts 3-5. You can't get a number from a boolean array, but you can pick a random number in the range and check whether the corresponding value in the array is true or false.

Parts 3 and 4 are the same thing, with different numbers. If you write 3, you can get 4 by adding 5 to the number generated. That's not good OO style, it's more of a C-style hack, but go ahead and do it anyway. Part 5 is really part of 3/4, which again are the same thing.

after a bit more playing around with my code i've managed to sort of fix point 3)
here's my code:

boolean seats[] = new boolean[11];
Random randomseat = new Random();
		for (counter = 1;counter < 2; counter++){
		System.out.println("**********************************");
		seats [5] = randomseat.nextBoolean();
		System.out.println(seats + "");
			}

EDIT: FIXED My first problem just changed .nextInt to .nextBoolean however when i run the program it displays this:
[Z@173a10f
instead of a number, i'm sure it's another silly mistake

[Z@173a10f is an address in memory. When you try to convert an array to a String, it calls the toString method, which for an array is the default Object.toString, which just prints the object's memory address.
If you want to print an element of an array, you have to supply the index, if you want to print all of the elements you have to iterate the array with a for loop, or an enhanced for loop.

seats [5] = randomseat.nextBoolean();

What does this do? What variable does it change, and what does it set it to?

seats [5] = randomseat.nextBoolean();

seats [] is my boolean array
i was hoping to get 1 random number between 1-5 however it gave me an error when i wrote it like this

seats []= ranseat.nextBoolean (5)

And yes i only want to print 1 random element in the array for exemple:

seats[5] = false
if it gets chosen by my random number generator
seats[5] = true and cannot be chosen again

I think you might be getting ahead of yourself. Go back to

Sketch out the procedure, without using any java.

And state more precisely what you're trying to do, this time walking through the details: Someone enters a number, either 1 or 2. What happens then?

It involves random numbers and an array of booleans, we've got that much, but what exactly happens when I push "1"?

let's say this is a bus tour application

if you press 1 you will be given a seat in the front of the bus or first class section
if you press 2 you will be at the back

this is fine i have done it, now the problem i have is with the boolean array, let's call it boolean seat[] = new boolean[11] //there's 10 seats but i start from 1 not 0.
customer presses 1 ----> you will be at the front of the bus
now i want to give the customer a seat number so i use the Random package/class? (don't know what it's called), the seat can only be used once.
once the seat has been chosen the boolean array = true.
e.g - seat[5] = false //it has not be chosen yet.
customer enters 1 ----> your seat is [5]
seat[5] = true //chosen and cannot be used again.

Okay, let me see if I understand you:

You have an array of booleans, and since we're talking about the first time through, they're all false.
Customer has entered 1, so we're dealing with the front of the bus

1) get a random number (n) in the range 1-5 inclusive.
2) set seat[n] to true
3) report "Your seat number is n"

Is that right? Is there anything missing from that?

yes that is absolutely right but also once a seat has been given it cannot be selected again if you know what i mean. I've managed to do everything by using an int variable called number to get my random seat number but i don't want to do that, instead i want to use the boolean array.

I've managed to do everything by using an int variable called number to get my random seat number but i don't want to do that, instead i want to use the boolean array.

And that's where you lose me. What do you mean by this? What do you want to do with the boolean array? All the boolean array can do is keep track of eleven values, and tell you (when you ask) whether they are true or false.

I know what I'd do with the boolean array here, but you have to figure out what you want to do with it before you write the program.

ok let's just forget my last post and go back to this

Okay, let me see if I understand you:

You have an array of booleans, and since we're talking about the first time through, they're all false.
Customer has entered 1, so we're dealing with the front of the bus

1) get a random number (n) in the range 1-5 inclusive.
2) set seat[n] to true
3) report "Your seat number is n"

Is that right? Is there anything missing from that?

And once a number(or seat in this case) has been assigned it cannot be chosen again and like you said is set to true.

So you're using the boolean array to keep track of which seats have been assigned. That means you have to add something to that procedure:

1) get a random number (n) in the range 1-5 inclusive.
---> how do we know if this seat has already been assigned? What do we do if it has been?
2) set seat[n] to true
3) report "Your seat number is n"

1) get a random number (n) in the range 1-5 inclusive.
---> how do we know if this seat has already been assigned? What do we do if it has been?
2) set seat[n] to true

3) report "Your seat number is n"

The bold part is what's giving me a hard time.

The bold part is what's giving me a hard time.

Yes, that's true.

Okay, let's suppose you've assigned some number of seats, and now you're going into this loop again. You get a number from 1 to 5, say it's 3. Now, suppose that number has been assigned already. What is the state of seats[3]?

if seats[3] has already been assigned then its state would be true.

Okay, so if it's state is true, we can't assign it. What then?
Contrariwise, if it's state is false, we can assign it. What then?

if it's state is true it has already been assigned therefore the program will look for another seat, one that has not been assigned (state still at false). Once an empty seat has been found it will be assigned to the next customer and so on until all seats have been assigned.

That's the idea. A while loop is probably your best bet for generating the random number. In pseudocode:

while (we haven't found an empty number yet)
  pick a number

mark the number as used
tell the passenger what his number is

There's a problem with this. We've missed one important detail - what is it?

the boolean array?

No... the array is what you're using to keep track of which numbers are used, remember?

Okay, try to write the code for what we've got so far, the bug in it will come to you. It can be fixed, and you'll certainly find it.

Once again:

pick a number from 1 to 5
check to see if the number is marked as used
keep doing that (while loop) until you get a number that hasn't been used.

When you get a number that hasn't been used:
mark it as used
tell the user his seat number.

Here's my code, the error is on line 19.

import java.util.Scanner;
import java.util.Random;

public class bus {
	public static void main (String args[]){
		
	
		int choice = 0;
		int number[] = new int[11];
		
		System.out.println("Select 1 for front of bus\nSelect 2 for back of bus");
		Scanner input = new Scanner(System.in);
		choice = input.nextInt();
		
		if (choice == 1){
		System.out.println("Your seat will be at the front of the bus");
		for (int counter = 1;counter < 2; counter++){
		Random randomseat = new Random();
		number = 1+randomseat.nextInt(5); //random number gen starts at 1 & ends at 5
		System.out.println("You have been assigned seat number "+number);}
		}
	}
}

And what's the problem with line 19? What error are you seeing?
Oh, I see. number is an array of int, not an int, which is why it won't compile. You can't assign to number, you have to assign to one of the elements of number.

You know, I hate to say it, but you're just not getting the point here.

Remember the boolean array? How you were going to use it to mark a seat as used? Go back to that. Get a random number, and look at that element of the array. If it's true, go back to "get a random number". Repeat until you find a "false" spot in the array.
Now, and only now, set the spot in the array to "true" and tell the user what their seat number is.


Maybe you should try it on paper: draw six boxes on a piece of paper. (six, so you can use an ordinary six-sided die). Try to see how you'd use a die and those six boxes to assign random seats, marking off the seats as you assign them.

alright guys i've made some progress since then, managed to get everything working except one tiny bit. Once the seat has been assigned the boolean does not change from false to true. What am I doing wrong?

if (choice == 1){//First Class method body starts here
			System.out.println("You have chosen the First Class");
		System.out.println("Seats\tValue");	//table to be displayed
		for (counter = 1; counter < seats.length; counter++){//loops from 1 to seats.lengh which is equal to 11
			seats [counter] = false;
		System.out.println(counter+"\t"+seats[counter]);	//prints out table
			if (counter == 5)
				break;	}	//once number 5 is reached counter stops
			if (number <= 4){ //as long as the value is smaller than 4 (which actually is 5 because it starts counting from 0) choose a number
		System.out.println("**********************************");
		seats [(int)counter] = true;
		number++;		//increment number by 1
		System.out.println("Your seat number is "+number+"\nYou will be placed in the First Class section"); //assigns seat to customer
		System.out.println("*************************************************************");
		}//end of for loop

What happens in the block of code beginning at line 4 and ending at line 8?

What is the state of the seats array at the end of this block?

Then, what happens in the block from line 9 to line 15?
(and what is "number" and where does it come from?)

In the block of code between line 4-8 I get a loop of number from 1 to 5 and they are printed in a table.

At the end of the block the state of seats array is at false

From line 9 to 15 I get a number (starting from one) and it increments for each customer. e.g user one is given seat 1, user 2 is given seat 2...
On line 11 i'm trying to change the state of my array to true for the number which has been given to the customer.

In the block of code between line 4-8 I get a loop of number from 1 to 5 and they are printed in a table.

At the end of the block the state of seats array is at false

And your problem was:

Once the seat has been assigned the boolean does not change from false to true.

So if you're clearing the array back to false for each customer, would it be surprising to find that your state is not preserved?

ok fixed it, just removed this line

seats [counter] = false;

replaced counter with number on line 11 and placed it after number++.

Thanks for all the help jon.kiparsky must have been really frustrating to deal with a noob like me :) you're a legend mate.

No bother. Carry on.
(you might want to mark this solved)

commented: long strory friend +8
Be a part of the DaniWeb community

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