0

my teacher got me this exercise to create a calculator which can store operations to a file and read them out later. I got the writer and I got the calculator; but the writer somehow overwrites everything in the file instead of adding. I tried bufferedWrtier append and now I am trying the Printwriter.

Debugging shows that my list is getting filled properly; yet the result is, that the saved file only contains the last input number and the last input operator.

public class Rechnung {
static Scanner scanner = new Scanner(System.in);
static double zahl1, zahl2, ergebnis;
static String op;
static boolean abbruch = true;
static File fileName = new File("Therme.txt");
static String numbers1;
static String eingabe;

public static void eingabe() {

    while (abbruch) {
        System.out.println("Geben Sie eine Zahl ein: ");
        Listen.numbersDouble.add(scanner.nextDouble());
        System.out.println("Bitte entscheiden Sie sich für '+', '-', '/', '*', '=': ");
        op = scanner.next();
        Listen.operators.add(op);

        if (op.equals("=")) {
            for (int i = 0; i < Listen.operators.size(); i++)
            {
            Writer.write(String.valueOf(Listen.numbersDouble.get(i)), Listen.operators.get(i));
            }
            abbruch = false;

        }

        else {
        }

    }
    System.out.println(ausgabe());
}

public static double rechnen(String op, double zahl1, double zahl2)

{

    switch (op) {
    case "+":
        ergebnis = zahl1 + zahl2;
        return ergebnis;
    case "-":
        ergebnis = zahl1 - zahl2;
        return ergebnis;
    case "/":
        ergebnis = zahl1 / zahl2;
        return ergebnis;
    case "*":
        ergebnis = zahl1 * zahl2;
        return ergebnis;
    }
    return ergebnis;
}

public static double ausgabe() {

    zahl1 = Listen.numbersDouble.get(0);

    for (int i = 1; i <= Listen.numbersDouble.size(); i++)

    {
        op = Listen.operators.get(i - 1);

        if(op.equals("=")) 
                {
            return zahl1;
                }
        zahl2 = Listen.numbersDouble.get(i);
        zahl1 = Rechnung.rechnen(op, zahl1, zahl2);


    }
    return -80085;
}

}

public class Writer {

public static void write(String string, String op) {
final String FILENAME = "C:\\Users\\nowackan\\eclipse-workspace\\Test.txt"; {


    BufferedWriter bw = null;
    FileWriter fw = null;

    try {

        fw = new FileWriter(FILENAME);
        bw = new BufferedWriter(fw);
        PrintWriter out = new PrintWriter(bw);
        out.println(String.valueOf(string));
        out.println(op);


        System.out.println("Done");

    } catch (IOException e) {

        e.printStackTrace();

    } finally {

        try {

            if (bw != null)
                bw.close();

            if (fw != null)
                fw.close();

        } catch (IOException ex) {

            ex.printStackTrace();

        }

    }

}

}

Please note that I have barely a few weeks of programming experience and do not know any programming conventions.

1

4 Answers 4

2

To append content to an existing file, open file writer in append mode by passing second argument as true.

 fw = new FileWriter(FILENAME,true);
 bw = new BufferedWriter(fw);
 PrintWriter out = new PrintWriter(bw);
 out.println(String.valueOf(string));
 out.println(op);
Sign up to request clarification or add additional context in comments.

Comments

2

You need to use the constructor of FileWriter(File, boolean append) in your Writer class.

Comments

2

You need to use the "append" parameter in the Filewriter constructor, which the valued must be true.

FileWriter fwr = new FileWriter(FILEN,true); 

Comments

1

Use this constructor instead of the one you are using to create the FileWriter, new FileWriter(FILENAME, true);

This is the java doc for this constructor and please read the append parameter of it,

/**
 * Constructs a FileWriter object given a file name with a boolean
 * indicating whether or not to append the data written.
 *
 * @param fileName  String The system-dependent filename.
 * @param append    boolean if <code>true</code>, then data will be written
 *                  to the end of the file rather than the beginning.
 * @throws IOException  if the named file exists but is a directory rather
 *                  than a regular file, does not exist but cannot be
 *                  created, or cannot be opened for any other reason
 */

Comments

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.