Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Can someone help me with this problem in my Java program? Should be good practice!

Hi, first of all, I've really been enjoying the Java course and am looking forward to hopefully being an entry level developer sometime soonish. I am trying to write my own program to help solidify my understanding on the concepts taught in the Treehouse courses. It's a reservation system for hostels. Anyway, I'll describe what my program SHOULD be doing, and then the problem. I've been studying for two weeks, so apologies if it's something obvious.

DESCRIPTION:

Upon running, my constructor for the room in the hostel takes two arguments; a name for that room (String) and the number of beds in that room (int). For testing purposes, I have made the name of that room "A", and I have given it 12 beds.

The beds in the room are stored in a Map. The key is a String, and the value is a Boolean. The String is the name of the bed, made of the room name and a number. (So in my example, room A would have 12 beds in, called A1, A2, A3 etc).

The value is a Boolean indicating whether or not that bed is available. All beds are available (true) by default.

PROBLEM:

The room constructor should automatically create the Map of beds upon constructing the room. However, my program doesn't run ANYTHING unless I comment out the room constructor. You can see some output on Main that I am using to demonstrate this.

Here is my Main class

package com.hostelwizard;

import com.hostelwizard.model.ReservationBook;
import com.hostelwizard.model.Room;
import com.hostelwizard.model.Session;

public class Main {

    public static void main(String[] args) {
        ReservationBook reservationBook = new ReservationBook();
        Session session = new Session(reservationBook);
        Room room = new Room("A",12);

        System.out.printf("This won't display unless room constructor " +
                            "is commented out.");







    }


}

Here is my Room class

package com.hostelwizard.model;

import java.util.Map;
import java.util.TreeMap;

public class Room {
    private int noOfBeds;
    private String roomName;
    private Map<String, Boolean>mBeds;

    public Room(String roomName, int noOfBeds){  //Room constructor

        int bedNumber = 1; //The first bed constructed will be labelled 1
        Boolean isAvailable = true; //All beds are 'available' by default
        int counter = noOfBeds; //A counter for the do-while loop to work on
        Map<String,Boolean> mBeds = new TreeMap<>();//To keep beds ordered

        do{
            //Creates a bed and gives it a name formed by having the room name ("A")
            //concatenated with the bed number (which increments from 1)
            //so the first bed should be called "A1"
            mBeds.put(roomName + String.valueOf(bedNumber),isAvailable);
            bedNumber++;//Increments bedNumber so next bed is "A2" etc


        }while(counter > 0);//Will do this until all beds are created


    }


}

I think you should give TreeMap same type as Map<String,Boolean> like

Map<String,Boolean> mBeds = new TreeMap<String, Boolean>();//To keep beds ordered

Does the log window output any errors, or give any clues?

3 Answers

Hey man, dont know where the code fails, we could try commenting this out and check to see if it runs

    do{
            //Creates a bed and gives it a name formed by having the room name ("A")
            //concatenated with the bed number (which increments from 1)
            //so the first bed should be called "A1"
            mBeds.put(roomName + String.valueOf(bedNumber),isAvailable);
            bedNumber++;//Increments bedNumber so next bed is "A2" etc


        }while(counter > 0);//Will do this until all beds are created 

or you could try using toString instead

mBeds.put(roomName + bedNumber.toString() ,isAvailable);

or make sure the condition doesnt run infinite, it must have a terminating condition. while(counter > bedNumber); or add counter --; statement inside the do while loop

while(counter > 0);//Will do this until all beds are created

Hmm. That hasn't worked. The strange thing is, I'm running this on IntelliJ and it's not giving me ANYTHING. No errors, nothing. Thanks for the help, though.

Yes! Your answer worked.. I was running an infinite loop!! But your answer isn't coming up as an actual answer on here. If you retype the answer in the answer bit I'll check it as the best answer.