Home > Mobile >  Delete a row in the java file if it meets the condition
Delete a row in the java file if it meets the condition

Time:11-10

I am doing a practice, in which I have to ask the user for the department number and then delete the line that contains the department ID. So far I have only managed to remove the entire line, but not by department number if not by line number. The file contains the following information:

1 | bird | Barcelona
2 | rabbit | Dublin
3 | turtle | Malaga
4 | bird | Madrid
7 | turtle | Dublin

my code is the following:

public static void borrarLinea() throws IOException {
        File inputFile = new File("Departamentos.dat");
        File tempFile = new File("DepartamentosTemp.dat");
    
        BufferedReader reader = new BufferedReader(new FileReader(inputFile));
        BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
        
        Scanner entrada = new Scanner(System.in);
        int lineToRemove;
        
        System.out.println("¿Que número de departamento deseas borrar?");
        lineToRemove = entrada.nextInt();
        entrada.nextLine();
        
        String currentLine;
        int count = 0;
    
        while ((currentLine = reader.readLine()) != null) {
            count  ;
            if (count == lineToRemove) {
                continue;
            }
            writer.write(currentLine   System.getProperty("line.separator"));
        }
        writer.close();
        reader.close();
        inputFile.delete();
        tempFile.renameTo(inputFile);
    }

CodePudding user response:

You may split your line on space, convert the first element to an int and compare to the user input

while ((currentLine = reader.readLine()) != null) {
    String[] parts = line.split(" ");
    if (Integer.parseInt(parts[0]) == lineToRemove) {
        continue;
    }
    writer.write(currentLine   System.getProperty("line.separator"));
}

CodePudding user response:

this helped me

public static void borrarLinea() throws IOException {
        File inputFile = new File("Departamentos.dat");
        File tempFile = new File("DepartamentosTemp.dat");

        BufferedReader reader = new BufferedReader(new FileReader(inputFile));
        BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
        
        Scanner entrada = new Scanner(System.in);
        int lineToRemove;
        
        System.out.println("¿Que número de departamento deseas borrar?");
        lineToRemove = entrada.nextInt();
        entrada.nextLine();
        
        String currentLine;
       
        while ((currentLine = reader.readLine()) != null) {
            String[] line_arr = currentLine.split("|");
            int num_linea = Integer.parseInt(line_arr[0]);
            if(num_linea == lineToRemove){
                 continue;
            }else{
                writer.write(currentLine   System.getProperty("line.separator"));   
            }
            
        }
        writer.close();
        reader.close();
        inputFile.delete();
        tempFile.renameTo(inputFile);
    }
  • Related