0

I'm trying to read stored values from a file into an ArrayList, problem is that every value is assigned correctly to its object except one that is somehow being applied to every other object in the list. Code section looks like this

        while(k<size) {
            int skiper=7*k;
            Float troubleshooting=0.0f;
            try {
                troubleshooting= Float.valueOf(Files.readAllLines(Paths.get("src/Reserva/datos.txt")).get(4+skiper));
            }catch(NumberFormatException e){
                troubleshooting=0.0f;
            }
            if(troubleshooting!=0.0f) {
                String reservation1 = Files.readAllLines(Paths.get("src/Reservation/data.txt")).get((1+skiper));
                String reservation2 = Files.readAllLines(Paths.get("src/Reservation/data.txt")).get((2+skiper));
                String reservation3 = Files.readAllLines(Paths.get("src/Reservation/data.txt")).get((3+skiper));
                Float fee = Float.valueOf(Files.readAllLines(Paths.get("src/Reservation/data.txt")).get((4+skiper)));
                String reservation4 = Files.readAllLines(Paths.get("src/Reservation/data.txt")).get((5+sskiper));
                int ubi=Integer.valueOf(reservation4);
                
                listareservas.set(ubi-1,new Booking(reservation1,reservation2,reservation3,reservation4,fee));
                System.out.println(reservationlist.get(k).getfee());
            }

Data is stored vertically in separated lines like this

-name -date1 -date2 -fee -room -price -reservationcode

booking looks like this

public class Booking {
    private static float fee;
    private String nroom;
    private static String initialdate;
    private static String finaldate;
    private String customername;
    private static float price;
    private String code;
    private String firstname;
    
    public Booking(String name,String indate, String findate, String roomn,float fe) {
        String[]namevector= name.split(" ");
        customername=name;
        firstname=namevector[0];
        initialdate=indate;
        fechafinal=indate;
        nroom=roomn;
        fee=fe;
        price=time.fee(initialdate, finaldate, fee);
        code=namevector[0]+roomn;

every value is stored correctly, but the fee of the last element is assigned to every other object in the ArrayList.

For example:

fee1=4000

fee2=5000 after the while ends fee1=5000 and everything happens after the while ends, so I know that the problem is inside this block.

Krosvick
  • 11
  • 3
  • Post `booking`. Without that, we can only guess. Also, use `listareservas.add` (not `listareservas.set`). And what is `reservationlist`? – Elliott Frisch Aug 14 '21 at 00:53
  • Why are you reading the "src/Reservation/data.txt" file a zillion times? Your program only *needs* to read it once. (Hint: assign the lines to a variable ...) – Stephen C Aug 14 '21 at 00:55
  • @ElliottFrisch reservationlist is the name of the arraylist, I also added the booking constructor. – Krosvick Aug 14 '21 at 01:10
  • Some of your fields are static. Static fields belong to the class, not to instances of the class, so they have only one value. Every instance of your class uses the same `fee` variable. – tgdavies Aug 14 '21 at 01:14

0 Answers0