0

the code below is from a reference i saw online, so there might be some similarities i'm trying to implement the code to remove an entire line based on the 1st field in this instance it is (aaaa or bbbb) the file which has a delimiter "|", but it is not working. Hope someone can advise me on this. Do i need to split the line first? or my method is wrong?

data in player.dat (e.g)

bbbb|aaaaa|cccc
aaaa|bbbbbb|cccc

Code is below

public class testcode {

public static void main(String[] args)throws IOException
{
    File inputFile = new File("players.dat");
    File tempFile = new File ("temp.dat");

    BufferedReader read = new BufferedReader(new FileReader(inputFile));
    BufferedWriter write = new BufferedWriter(new FileWriter(tempFile));

    Scanner UserInput = new Scanner(System.in); 
    System.out.println("Please Enter Username:");
    String UserIn = UserInput.nextLine();

    String lineToRemove = UserIn;
    String currentLine;

    while((currentLine = read.readLine()) != null) {
        // trim newline when comparing with lineToRemove
        String trimmedLine = currentLine.trim();
        if(trimmedLine.equals(lineToRemove)) continue;
        write.write(currentLine + System.getProperty("line.separator"));
    }
            write.close();
            read.close();
            boolean success = tempFile.renameTo(inputFile);
    }
}
1
  • Is this code supposed to do what you want? If not, please include the code you have tried to solve your problem. Commented Apr 8, 2015 at 9:37

1 Answer 1

2

Your code compares the entire line it reads from the file to the user name the user enters, but you say in your question that you actually only want to compare to the first part up to the first pipe (|). Your code doesn't do that.

What you need to do is read the line from the file, get the part of the string up to the first pipe symbol (split the string) and skip the line based on comparing the first part of the split string to the lineToRemove variable.

To make it easier, you could also add the pipe symbol to the user input and then do this:

string lineToRemove = UserIn + "|";

...

if (trimmedLine.startsWith(lineToRemove)) continue;

This spares you from splitting the string.


I'm currently not sure whether UserInput.nextLine(); returns the newline character or not. To be safe here, you could change the above to:

string lineToRemove = UserIn.trim() + "|";
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thx for the advise :) it works but the renaming of the file does not work. i'll try and figure it out
Well, renaming a file only works if the destination file doesn't exist already. You need to delete the input file first and rename the temp file afterwords.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.