Trying to delete a line from a CSV file, my code deletes a line and half of the following line

anon12524 :

Hi I need to be able to delete a line in a CSV file. My code currently deletes a line and up to the first comma of the following line.

I've tried changing the scanner delimiter but I can't get it to work how I need it to.

public void deleteRecord() { 
        String filePath = "marks.txt";
        System.out.println("Enter the StudentID of the record you wish to delete: ");
        Scanner in = new Scanner(System.in);
        String removeTerm = in.nextLine();
        String tempFile = "temp.txt";
        File oldFile = new File(filePath);
        File newFile = new File(tempFile);

        try {
            FileWriter fw = new FileWriter(tempFile,true);
            BufferedWriter bw = new BufferedWriter(fw);
            PrintWriter pw = new PrintWriter(bw);
            Scanner scan = new Scanner(oldFile);
            scan.useDelimiter("[,]");

            while (scan.hasNext()) {
                String ID = scan.next();
                String Mark = scan.next();
                if (!ID.equals(removeTerm)) {
                    pw.println(ID + "," + Mark);
                }
            }
            scan.close();
            pw.flush();
            pw.close();
            oldFile.delete();
            File dump = new File(filePath);
            newFile.renameTo(dump);
            System.out.println("Record successfully deleted.");

        } catch(IOException e) {
            System.out.println("Error has occured.");

        }
    }
B00987,58
B00567,43
B00343,59
B00653,25
B00757,31
B00876,40
B00421,62
B00568,78
B00826,79
B00126,93
B00862,62
B00999,12
B00237,68
B00762,85
B00864,49

This is the file before deleting a line. I will try delete the first line.

43
B00343,59
B00653
25
B00757,31
B00876
40
B00421,62
B00568
78
B00826,79
B00126
93
B00862,62
B00999
12
B00237,68
B00762
85
B00864,49

This is what happens.

I need it to look like this.

B00567,43
B00343,59
B00653,25
B00757,31
B00876,40
B00421,62
B00568,78
B00826,79
B00126,93
B00862,62
B00999,12
B00237,68
B00762,85
B00864,49

Could someone help me fix this problem?

TheWhiteRabbit :

Your input file only contains 1 comma per record, though you scan 2 times to get the ID and mark. So the first scan will give you ID 'B00987' and mark '58 B00567'.

You could use the scanner and add multiple delimiters like this:

s.useDelimiter(",|\\n");

This way the scanner would scan up to comma's and new lines.

I don't use scanners very often though, in your case I would even prefer to use a BufferedReader:

  public void deleteRecord() { 
        String filePath = "marks.txt";
        System.out.println("Enter the StudentID of the record you wish to delete: ");
        Scanner in = new Scanner(System.in);
        String removeTerm = in.nextLine();
        String tempFile = "temp.txt";
        File oldFile = new File(filePath);
        File newFile = new File(tempFile);

        try {
            FileWriter fw = new FileWriter(tempFile,true);
            BufferedWriter bw = new BufferedWriter(fw);
            PrintWriter pw = new PrintWriter(bw);
            BufferedReader reader = new BufferedReader(new FileReader(oldFile);

            String line = "";
            while((line = reader.readLine()) != null) {
              if(!line.startsWith(removeTerm)) {
                pw.println(line);
              }
            }
            reader.close();
            pw.flush();
            pw.close();
            oldFile.delete();
            File dump = new File(filePath);
            newFile.renameTo(dump);
            System.out.println("Record successfully deleted.");

        } catch(IOException e) {
            System.out.println("Error has occured.");

        }
    }

Also using autocloseables or at least manage the resources properly would be a nice optimization.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=136064&siteId=1