0

I'm trying to make a program that gives marks for student.

First it should ask the student id, after that you need to give mark each of the criteria to the same student.

This code doesn't change anything after i gave the marks.

BufferedReader br = new BufferedReader(new FileReader("project.csv"));
while ((line = br.readLine()) != null) {
    String[] cols = line.split(",");
    System.out.println("Please choose a criteria (2-7) ?");
    int subjectToGiveMark = in .nextInt(); // for creativity is 2
    System.out.println("Please enter a mark :");
    int mark = in .nextInt(); // which mark should be given 
    final int size = cols.length;
    String[] finalResult = new String[size];
    int index = 0;

    while (index < finalResult.length) {
        if (index == subjectToGiveMark) {
            finalResult[index] = mark + "";
        } else {
            finalResult[index] = cols[index];
        }
        index++;
    }
}

Can anyone tell me what is wrong with it ? enter image description here

Sigma Octantis
  • 903
  • 1
  • 8
  • 25

1 Answers1

0

First of all, you should read and/or write your files with try with resources, for safety, as you can forget to close the file, or even an exception can prevent you from doing so.

The try-with-resources statement ensures that each resource is closed at the end of the statement.

- The Java™ Tutorials

More info: https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

How? For example, in what you're reading, just wrap-it with a try:

try (BufferedReader br = new BufferedReader(new FileReader("project.csv"))) {
  // The while code here...
}

Also you're modifying the finalResult variable, but not doing anything with it, so your changes are just stored there and nothing more, that's why you're not seeing your changes!

You should create a variable outside of the while loop, storing all the lines, like a list. Otherwise you can open another file (Eg: project-output.csv) and write it while reading the other file.

// Same principle as reading
try (BufferedWriter writer = new BufferedWriter(new FileWriter("project.csv"))) {
  // Write the result
}

This answer addresses the subject of writing in more detail: https://stackoverflow.com/a/2885224/1842548

Example of read-write, I'm assuming Java 8:

try (BufferedWriter writer = new BufferedWriter(new FileWriter("project-output.csv"))) {
  try (BufferedReader reader = new BufferedReader(new FileReader("project.csv"))) {
    String line;
    while ((line = reader.readLine()) != null) {
        String[] cols = line.split(",");
        System.out.println("Please choose a criteria (2-7): ");
        final int subjectToGiveMark = in.nextInt(); // for creativity is 2
        System.out.println("Please enter a mark: ");
        final int mark = in.nextInt(); // which mark should be given
        cols[subjectToGiveMark] = Integer.toString(mark);
        // Here is where you write the output:
        writer.write(String.join(",", cols));
        writer.newLine();
    }
    writer.flush();
  }
}

You can see a working example on repl.it https://repl.it/repls/ScaredSeriousCookie

Community
  • 1
  • 1
Sigma Octantis
  • 903
  • 1
  • 8
  • 25