Results 1 to 9 of 9
  1. #1
    rybo85 is offline Member
    Join Date
    May 2012
    Posts
    5
    Rep Power
    0

    Default Array of object initialization

    Getting errors with the following code:
    /** Creates Hotel Class */


    public class Hotel
    {

    /** Creates instance variables */
    private String name;

    private String location;

    private int occupiedCnt;


    private boolean Full;




    /** Default constructor for Hotel */
    public Hotel()
    {

    Room[] theRooms = new Room[5];
    }


    /** Paramterized Constructor */
    public Hotel(String name, String location)
    {

    this.name = name;
    this.location = location;

    }


    public String addRoom(int roomNum, String bedType, char smoking, double rate)
    {


    Room[] theRooms = new Room [5];

    Room[0]= new Room (101, "Queen", 'n', 100);
    Room[1]= new Room (102, "King", 'n', 110);
    Room[2]= new Room (103, "King", 's', 110);
    Room[3]= new Room (104, "twin", 'n', 90);
    Room[4]= new Room (105, "Queen", 'n', 100);


    }
    The error is :
    Hotel.java:24: error: cannot find symbol
    Room[] theRooms = new Room[5];
    ^
    PLease help

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Array of object initialization

    Where is the class Room defined? Please post the code.

  3. #3
    rybo85 is offline Member
    Join Date
    May 2012
    Posts
    5
    Rep Power
    0

    Default Re: Array of object initialization

    The Room class is a separate class that is called by the Hotel Class. The project is to create a Hotel that has an array of Rooms created by the Room class. THe addRoom method assigns the parameters for each Room. The code I have looks like this:
    /** Creates room class */

    public class Room
    {


    private int roomNum;
    private String bedType;
    private double rate;
    private String occupantName;
    private char smoking;
    private boolean occupied;


    public Room()
    {

    roomNum = 0;
    bedType = " ";
    rate = 0;
    occupantName = "";
    smoking = 0;
    occupied = false;

    }

    public Room(int roomNum, String bedType, char smoking, double rate)
    {

    this.roomNum = roomNum;
    this.bedType = bedType;
    this.smoking = smoking;
    this.rate = rate;

    }

    public String getBedType()
    {

    return bedType;


    }


    public char getSmoking()
    {

    return smoking;

    }

    public int getRoomNum()
    {

    return roomNum;

    }

    public double getRoomRate()
    {

    return rate;

    }

    public String setOccupant()
    {

    return occupantName;

    }

    public boolean setOccupied()
    {
    int len = occupantName.length();
    if (len > 1){
    occupied = true;
    }
    else if(len <1){
    occupied = false;
    }
    return occupied;

    }



    public boolean isOcuppied()
    {

    boolean getRoomState = occupied;
    return getRoomState; }
    public String toString(){
    return "Room Number: " + roomNum +"Name of Guest: " + occupantName +"Bed Type: " + bedType + "Smoking Room: " + smoking + "Rate: " + rate;


    }

    }

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Array of object initialization

    The code should be:

    Java Code:
    Room[] theRooms = new Room [5];
    
    theRooms[0] = new Room (101, "Queen", 'n', 100);
    ...
    ...
    ...

  5. #5
    rybo85 is offline Member
    Join Date
    May 2012
    Posts
    5
    Rep Power
    0

    Default Re: Array of object initialization

    Tried the suggestion but, it didn't solve the problem. Still getting error message:
    Hotel.java:24: error: cannot find symbol
    Room[] theRooms = new Room[5];
    ^
    symbol: class Room
    location: class Hotel

    Any other suggestions?

  6. #6
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Array of object initialization

    Do you have the Hotel.java and the Room.java in the same path? You are compiling using a javac command line or using an IDE?

  7. #7
    rybo85 is offline Member
    Join Date
    May 2012
    Posts
    5
    Rep Power
    0

    Default Re: Array of object initialization

    Using an IDE called jGRASP. Any suggestions? It seems that Hotel doesn't know that Room exists.

  8. #8
    rybo85 is offline Member
    Join Date
    May 2012
    Posts
    5
    Rep Power
    0

    Default Re: Array of object initialization

    Thanks for all the help. I relooked at how I used your suggestion and I got it to work.

  9. #9
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Array of object initialization

    So, what was that causing the problem? And how did you solve it?

Similar Threads

  1. Int Array Initialization Problem
    By dragstang86 in forum New To Java
    Replies: 5
    Last Post: 03-28-2012, 10:54 PM
  2. An Array of Objects (Class Initialization)
    By Chris_C in forum New To Java
    Replies: 9
    Last Post: 02-09-2011, 06:49 AM
  3. Replies: 23
    Last Post: 09-07-2010, 09:12 PM
  4. Array initialization difference
    By sln69 in forum Advanced Java
    Replies: 2
    Last Post: 10-10-2008, 03:38 AM
  5. 2D Array Initialization
    By M77 in forum Advanced Java
    Replies: 3
    Last Post: 06-04-2008, 03:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •