Reputation: 149
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
Upvotes: 1
Views: 116
Reputation: 17595
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;
}
Upvotes: 1