I am new to java and having an issue when trying to add a single element into a structure type array. I have my array setup as so: public apartment availableRoom[] = new apartment[1]; My main calls a method that initializes this as soon as the application launches:
availableRoom[0] = new apartment(150, 2, 200.00,null);
//this sets default values for room#, beds, price, and guest
My constructor takes the info like so
public apartment(int roomNum, int beds, double price, String guest )
{
this.roomNumber = roomNum;
this.roomBeds = beds;
this.nightlyFee = price;
this.roomGuest = guest;
}
Where I am having issues is when I am trying to assign a guest to the room. I am trying it with availableRoom[i].roomGuest = name Name is entered by the user and i is set to 0 (I checked). No errors but when I go to print the information for the room it returns every value as 0 and the guest to null. Can anyone see what I am doing wrong? (FYI Apartment is a separate class from main)
Main
public class apartmentMain {
static apartment action = new apartment();
public static void main(String[] args) {
action.createApt();
action.addGuest();
apartment.java
public void createApt()
{
availableRoom[0] = new apartment(150, 2, 200.00,null);
}
public void addGuest()
{
name = input.next();
availableRoom[i].roomGuest = name;
}
well, as you say
I think , you are setting values in diffrent object and printing different one. If you just paste how you are printing its values it may help a lot.
Things to Consider