0

I have a task to make simulator of examination. What I mean, student enter his first name, last name and birth after teacher input his marks in some subjects and then he decided does student passed subject or not. So, I have a question, how to write in file text with new line and read with rewriting this file(read line by line and rewrite). For example: I add a text like this in file Jimmy Kordon 05.10.1998 how to make new line for this, I mean after 1998 and then add another stroke. And it should looks like this:

Jimmy Kordon 05.10.1998
Alex Starr 12.09.2000

After this I call another method which rewrite into this

Jimmy Kordon 05.10.1998 200.0 yes
Alex Starr 12.09.2000 10.0 no

This is how I did it. I have a problem cause after rewriting I don't have a newline and if add new person this method just delete old info and then add new. What's the problem? Please help me.

It is a method for student to adding info(this method has student class).

public void write() throws IOException {
    Scanner in = new Scanner(System.in);
    System.out.println("Input name");
    String name = in.nextLine();
    setName(name);

    System.out.println("Input last name");
    String lastName = in.nextLine();
    setLastName(lastName);

    System.out.println("Input birthday");
    String birth = in.nextLine();
    setBirth(birth);

    String ex = getName() + " " + getLastName() + " " + getBirth();

    BufferedWriter writer = new BufferedWriter(new FileWriter("test.txt"));
    try {
        writer.write(getName() + " " + getLastName() + " " + getBirth());
        writer.newLine();
        //writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        writer.close();
    }
}

And this is a method which I call from teacher class to rewrite this info:

private void inputMarks() {
    Scanner in = new Scanner(System.in);

    System.out.println("Input Math mark");
    emM = new ExamMarks();
    emM.setMath(in.nextDouble());

    System.out.println("Input Physics mark");
    emP = new ExamMarks();
    emP.setPhysics(in.nextDouble());

    System.out.println("Input Ukrainian mark");
    emU = new ExamMarks();
    emU.setUkrainian(in.nextDouble());

    System.out.println("Input English mark");
    emE = new ExamMarks();
    emE.setEnglish(in.nextDouble());

}

private double Arithm(double Math, double Physics, double Ukrainian, double English) {
    double sum = Math + Physics + Ukrainian + English;
    double average = sum / 4.0;
    return average;
}

public void rewrite() {
    inputMarks();

    String a = read();

    System.out.println(Arithm(emM.getMath(), emP.getPhysics(), emU.getUkrainian(), emE.getEnglish()));

    Scanner in = new Scanner(System.in);
    System.out.println("Input yes/no");
    String YesNo = in.nextLine();

    try(BufferedWriter writer = new BufferedWriter(new FileWriter("test.txt"))) {
        writer.write(a + " " + Arithm(emM.getMath(), emP.getPhysics(), emU.getUkrainian(), emE.getEnglish())
                + " " + YesNo);
        writer.newLine();
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

1 Answer 1

1

Edit the code

try(BufferedWriter writer = new BufferedWriter(new FileWriter("test.txt")))

to

try(BufferedWriter writer = new BufferedWriter(new FileWriter("test.txt", true)))

This will enable the append mode instead of override the old data.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.