Search code examples
javafilecsvsavedelete-row

CSV file on java need to ask user what row to delete, delete it and update table?


Using Java I am trying to delete rows using user input, I want the code to delete the row specified and then update the file.

source screenshot

I have tried using different methods however there are too many errors in them.

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
B00786  85

My Code That is giving me an error:

if(!flag)
        pw.printline(line1);
    line1 = br1.readLine();
}
pw.flush();

br1.close();
pw.close();
    {
System.out.println("Student has been deleted sucsessfully");

} catch (Exception e) {


   System.out.println(e.getMessage());


} 
}

Solution

  • Untested But should be enough to go from here:

    void deleteStudent(String deleteLine)
    {
        ....
        String line;
        while((line = reader.readLine()) != null)
        {
            if (!line.equals(deleteLine))
            {
                pw.printline(line);
            }
        }
        pw.close();
    }
    

    There are a number of new (streams and NIO2) methods which would make the code more compact, but it requires later Java versions and/or is not beginner level (which can be argued).