Results 1 to 5 of 5
  1. #1
    Ronan23 is offline Member
    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0

    Default Enter number of rooms then call readRoom() method

    I need a bit of help, i want to be able to enter the number of rooms of a house using the keyboard, then whatever number i entered I want to call the readRoom() method which contains information on a room and will add a room to an arraylist. Any help would be great thanks.

    Java Code:
    import java.util.Scanner;
    import java.util.ArrayList;
    import java.io.*;
    import java.util.*;
    
    public class House {
      
      private int idNum;
      private static int internalCount = 0;
      private String address;
      private int numRooms;
    
      private String houseType;
      private String roomInfo;
      
    ArrayList <Room> roomlist=new ArrayList<Room> ();
    Room r=null;
    
    
    
    
    
      public House (String address, int numRooms, String houseType, String roomInfo)  {
        idNum = internalCount++;
    
        
        this.address = address;
        this.numRooms = numRooms;
        this.houseType = houseType;
        this.roomInfo = roomInfo;
       }
    
      
      //added this for convenience - could also have this code in PropertyMenu and call 3-arg cons
      public House () {
        idNum = ++internalCount;
    
        Scanner scan = new Scanner(System.in);
        scan.useDelimiter("\n");
    
        
        System.out.println("Enter address of house:");
        address = scan.next();
        
         System.out.println("Enter type of house:");
        houseType = scan.next();
        
        Scanner scan2 = new Scanner(System.in);
        
        System.out.println("Enter number of rooms:");
          numRooms = scan2.nextInt();
          System.out.println(numRooms);
          
          while (numRooms>0)
          {
           readRoom();
          }
    
          
       
      }
          
    
    
    
      int getIdNum() {
        return idNum;
      }
      
      public void readRoom () {
        Room r= new Room ();
        roomlist.add(r);
       }
      
    public double getHouseArea()
       {
          return r.getArea();
       }
        
    
    
    
     
     
        @Override
       public String toString() {
       String result;
    
          result = "ID number:\n" + idNum + "\n";
          result += "Address:\n" + address + "\n";
          result += "Type of House:\n" + houseType+ "\n";
          result += "Number of Rooms:\n" + numRooms + "\n";
          result += "Room Info:\n" + r + "\n";
          result += "House Area:\n" + getHouseArea() + "\n";
          
      
    
         return result;
       }
    }

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    748
    Rep Power
    2

    Default Re: Enter number of rooms then call readRoom() method

    I am not sure what your question is...

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,019
    Blog Entries
    7
    Rep Power
    20

    Default Re: Enter number of rooms then call readRoom() method

    I closed your other (identical) thread.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    748
    Rep Power
    2

    Default Re: Enter number of rooms then call readRoom() method

    What I noticed is that you have an endless loop here:
    Java Code:
    while (numRooms>0)
    {
       readRoom();
    }
    As long as numRooms doesn't change, the loop will not end. I think you want a for-loop here.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: Enter number of rooms then call readRoom() method

    Quote Originally Posted by JosAH View Post
    I closed your other (identical) thread.

    kind regards,

    Jos
    So did I.
    Which seemed to open it again.
    So I closed it again...stake through the heart this time.
    Please do not ask for code as refusal often offends.

Similar Threads

  1. Enter number of rooms then call readRoom() method
    By Ronan23 in forum New To Java
    Replies: 1
    Last Post: 03-14-2013, 02:20 PM
  2. Replies: 3
    Last Post: 03-11-2013, 02:03 PM
  3. how to enter input method name for getMethod(------)
    By Dayanand in forum New To Java
    Replies: 3
    Last Post: 02-23-2011, 06:07 PM
  4. Replies: 3
    Last Post: 01-01-2011, 02:36 AM
  5. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:37 AM

Posting Permissions

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