1

I'm trying to populate an ArrayList with data stored in a text file, the data is 5 different values separated by white space, and a mix of boolean, strings and integers. Also, I'm using BlueJ, not sure if that changes anything though.

When the data is read from the file, objects of type Room should be created based on that data

I am new to Java, I've just started learning it within the last few weeks, my read data class is as follows:

Room Data Class:

public class RoomData
{
   //Default Values of a Room
   private int roomNumber = 0;
   private int bookingNights = 0;
   private boolean hasEnSuite = false;
   private boolean isBooked = false;
   private String bookingName = "<None>";

   public void setRoomNumber(int roomNumber) 
   {
       this.roomNumber = roomNumber;
   }

   public void setBookingNights(int bookingNights)
   {
       this.bookingNights = bookingNights;
    }

   public void setHasEnSuite()
   {
       this.hasEnSuite = hasEnSuite;
   }

   public void setIsBooked()
   {
       this.isBooked = isBooked;
   }

   public void setBookingName()
   {
       this.bookingName = bookingName;
   }
   }

ReadDataClass:

public class ReadHotelData
{
    private String filePath;

    public ReadHotelData()
    {
        filePath = "hotelData.txt";
    }

    private List<RoomData> list = new ArrayList <>();

    public boolean hasNext() throws FileNotFoundException 
    {
        Scanner s = new Scanner(new File("hotelData.txt"));

        while (s.hasNext())
        {
           String nextLine = s.nextLine(); //reads text file line by line
           RoomData roomData = new RoomData();

           String[] values = nextLine.split(" "); // splits the text file by white space

           roomData.setRoomNumber(Integer.parseInt(values[0]));
           roomData.setBookingNights(Integer.parseInt(values[1]));

           roomData.setHasEnSuite(Boolean.parseBoolean(values[2]));
           roomData.setIsBooked(Boolean.parseBoolean(values[3]));

           roomData.setBookingName(String.parseString(values[4]));

           list.add(roomData);
        }// end loop
        s.close();
        return true;
    }

    public List <RoomData> getRoomDataList() 
    {
        return list;
    }
}

Like I said I'm new so if I'm missed anything I'd really appreciate any help!

Example of data stored in text file:

0 false David 0 false
0 true John 0 false
0 false Jim 0 true
15
  • 1
    So what are you having problems with? Commented Nov 27, 2015 at 21:50
  • I want an arraylist to be created when the code is ran, the arraylist is stored in another class called 'Hotel' link is how my project currently sits Commented Nov 27, 2015 at 21:54
  • Do you want to read each row into an element of the list? The code you have already reads all the token into the arraylist, You need to have a getter method which returns it so you can use it anywhere you want. Commented Nov 27, 2015 at 21:54
  • @A4L yes, the hotel arraylist contains rooms, and each of those rooms has 5 variables that are stored in the text file and when the program is booted up, it should read from this text file Commented Nov 27, 2015 at 21:55
  • @A4L do you mean like this? link I don't how what you mean by split and parse so i'll have to look that up. (Sorry I didnt know how to post code in here) Commented Nov 27, 2015 at 22:41

1 Answer 1

1

First create a class RoomData to hold the data for each room and give each variable a meaningful name along with the appropriate type.

Change your arraylist to hold that type instead of String

private List<RoomData> list = new ArrayList<>();

Read each line using s.nextLine()

while(s.hasNext())
{
    String nextLine = s.nextLine();
    RoomData roomData = new RoomData();

Create an instance of that class, split and parse each value into the corresponding variable in the instance of RoomData you have created.

  String[] values = nextLine.split(" ") // split by space
  // lets say you have "0 false David 0 false"
  // values[0] would be "0"
  // values[1] would be "false"
  // values[2] would be "David"
  // values[3] would be "0"
  // values[4] would be "false"

All the values in values would be of type String you will need to convert those from String to the type you have defined in RoomData, for int you can use Integer.parseInt(String s), for boolean there is a similar method (Boolean.parseBoolean(String s))[http://docs.oracle.com/javase/8/docs/api/java/lang/Boolean.html#parseBoolean-java.lang.String-], string values can be set directly.

Add that instance to the arraylist.

    list.add(roomData);

} // end of while

Add a getter method to return that list for use in other classes.

public List<RoomData> getRoomDataList() {
    return list;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Or make the RoomData constructor parse a line.
@Robert how would I do that?
@Aaranihlus Just make a constructor that takes a String, e.g., public RoomData(String line), then have parsing code like A4L showed above, using split and assigning to member variables as needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.