-1

I'm going through the following steps and I'm having an issue when saving the original file.

  1. Read file to objects
  2. Add objects to arraylist
  3. override toString method for printing to console
  4. save file repeat.

The issue is repeating as the file layout has now changed. I'm trying to find a method to print to the file without all of the appends.

I've looked into 2 different ways, ignore words while reading, and trying to remove the words before saving. Both of which seemed really messy and confusing.

Here is my StringBuilder:

    @Override
public String toString() {
    StringBuilder builder = new StringBuilder();
    builder.append("Room Number: ").append(roomNumber);
    builder.append(", Room Type: ").append(roomType);
    builder.append(", Room Price: £").append(format.format(roomPrice));
    builder.append(", Room Balcony: ").append(roomBalcony);
    builder.append(", Room Lounge: ").append(roomLounge);
    builder.append(", Room Reserved by: ").append(reservedBy);
    builder.append("\n");
    return builder.toString();
}

Here is my filescanner

        while (file.hasNextLine()) {
        int roomNumber = file.nextInt();
        String roomType = file.next();
        double roomPrice = file.nextDouble();
        boolean roomBalcony = file.nextBoolean();
        boolean roomLounge = file.nextBoolean();
        String reservedBy = file.next();
        roomList.add(new Room(roomNumber, roomType, roomPrice, roomBalcony, roomLounge, reservedBy));
    }
jarvo69
  • 7,908
  • 2
  • 18
  • 28
DMck
  • 50
  • 4
  • 3
    You forgot to explain the issue. – shmosel Dec 09 '16 at 04:24
  • Really unclear what the problem is. Are you asking about writing the file, reading the file, or printing its contents to the console (pick one)? Are you looking for [other ways to format strings](http://stackoverflow.com/questions/6431933/how-to-format-strings-in-java)? Are you looking for other file formats, like XML or something? What is the issue here? – Jason C Dec 09 '16 at 04:32
  • @JasonC struggling to save the file without appends, but looked into several different ways to do this. As the issue occurs when I'm trying to read the file a 2nd time and the save has changed the layout. – DMck Dec 09 '16 at 05:28

1 Answers1

0

I think I know what you are asking. You implemented a toString() that's useful for console print-outs but not for writing data to your files. This is fine, and not unusual. In your code, you're using toString() for informative/UI purposes, so you'll just have to not use toString() when you're saving it to a file.

So just... save it to the file another way. It's pretty much that simple.

You could for example add a function that builds a string you can save, like:

public String toFileString () {
    return the line you'd write to a file;
}

Then

ArrayList<MyThings> things = ...;

for (MyThing thing : things) 
    outputFile.println(thing.toFileString());

Or you could write a function that writes to a PrintWriter (or some other Writer but PrintWriter has a lot of convenient methods), for example:

void saveTo (PrintWriter out) {
    // print a line to out
}

Then:

for (MyThing thing : things) 
    thing.saveTo(outputPrintWriter);

Or you could make your object have no knowledge of files at all and instead just write out its stuff explicitly when saving:

for (MyThing thing : things) {
    out.print(thing.getRoomNumber());
    // and so on...
}

Or whatever. There's a million options, point being, just don't save with toString(), save with something else.

Jason C
  • 38,729
  • 14
  • 126
  • 182
  • Really informative answer, thank you very much! I'll give it a go, leaning more towards the 2nd option at the moment. I'll get back to you and let you know how I got on shortly. – DMck Dec 09 '16 at 04:45
  • File is ending up getting wiped when save is called. Function: void saveTo (PrintWriter out) throws FileNotFoundException { PrintWriter save = new PrintWriter(new FileOutputStream("rooms.txt")); save.println(roomNumber + " " + roomType + " " + roomPrice + " " + roomBalcony + " " + roomLounge + " " + reservedBy); } And then trying to use it like this. for (Room r : roomList){ r.saveTo(save);} Not giving any errors, just completely wiping the file. – DMck Dec 09 '16 at 05:24
  • @DMck Well if you open a new file every time you save an object, you should expect that. You're passing `PrintWriter out` to `saveTo`, so use it. Don't make a new `PrintWriter` or open a new file each time. Just do it once and pass it to `saveTo`. – Jason C Dec 09 '16 at 05:33
  • Resolved the issue there (beginner at java so apologies). Just trying to remove a blank line at the end of the save now. Marked yours as answer though, thank you. – DMck Dec 09 '16 at 16:15