Take the 2-minute tour ×

I'm stuck on an array problem and could use some help.

The first visitor will occupy the middle position:

        _ _ _ _ _ X _ _ _ _

The next visitor will be in the middle of the empty area on the left side, as that is the longest sequence of unoccupied places:

        _ _ X _ _ X _ _ _ _


I have made a good start and need help continuing. Here's my code and please help me. I have the boolean array already created but I cant figure out how to print out the contents of it or to look and see it so I know which elements are true or false so I can fill out the rest of the array.

import java.util.Scanner;
public class Homework06 {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner (System.in);
        int answer = 0;
        String start="";

        int[] stalls=new int[answer];
            boolean firstPerson[]=new boolean[answer];
        occupyOneMore(answer, start, firstPerson);

    public static int promptForNumberOfStalls (){
        int answer =0;
        Scanner keyboard = new Scanner (System.in);
        System.out.println("Hello, how many stalls will there be");
        answer = keyboard.nextInt();
        return answer;
    public static String firstPerson( int answer){
        boolean[] firstPerson = new boolean[answer];
        String start="";
        for(int i=0;i<firstPerson.length;i++){
        return start;

    public static boolean[] occupyOneMore(int answer, String start, boolean[] firstPerson){
    int middle=answer/2+1;
        if (firstPerson[i] == true) {
            middle = (int) Math.floor(i / 2); // use Math.ceil to round up



    return firstPerson;


share|improve this question
Welcome to stackoverflow. You haven't asked a specific question. My strong advice is to create an SSCCE. Include specifics like sample I put and expected output. –  Bohemian Apr 9 at 3:19
add comment

3 Answers

To print the contents of an array, use Arrays.toString():

boolean[] ba = new boolean[]{true,true,false,true};

will produce the output

share|improve this answer
add comment
int answer = 0;
boolean firstPerson[] = new boolean[answer];

The first problem is your array starts off with a length of 0. Arrays are fixed so you gotta define the size.

PS: us doing your homework for you won't help.

Edit: with regards to your comment

You have to find middle of the left side. So loop through the array and find the size until you hit a true value.

int middleLeft;
    if (firtPerson[i] == true) {

once you have the middleLeft variable put a true value at that location in the array.

share|improve this answer
Thank you very much. I always forget to move my array after I get the answer from a method. Do you have any tips on where to go in the last method? –  user2206045 Apr 9 at 3:15
Tell me what you want done and I'll see if I can help. –  Joban Dhillon Apr 9 at 3:17
I'm not asking for answers, just a hint on what I need to focus on next. I have been staring at this for hours and just want an incentive to keep going. –  user2206045 Apr 9 at 3:18
So far I have created a boolean array so I can know which positions have already been filled and I have got the answer from the user. Right now Im trying to fill the boolean array with the positions of the filled stalls and im stuck. –  user2206045 Apr 9 at 3:20
I made an edit, see if it helps. –  Joban Dhillon Apr 9 at 3:24
add comment

I believe you can use something like this for printing values inside your loop:

System.out.println(firstPerson[s] ? "true" : "false");

or use System.out.print(...); to keep values on the same line.

share|improve this answer
ok. I have put all of your code to use and now im tryin to make "middle" register as true. Edited. –  user2206045 Apr 9 at 3:44
add comment

Your Answer


