-3

this is in java

public void deleteUser(String username) {
        File f1 = new File("index.txt");
        Scanner f1reader = new Scanner(f1);
        String.replaceAll(username, "");
    }

The above code is intended to delete a username from a .txt file named index based on the input to the function however I receive the following error on the replaceAll line.

Cannot make a static reference to the non-static method replaceAll(String, String) from the type String
  • Look at the String API to see how to use `replaceAll`. It is called on a String *object*, not the String class, and it returns the String of interest. – Hovercraft Full Of Eels May 23 '22 at 10:23
  • 1
    After you read the linked duplicate also read [my previous comment](https://stackoverflow.com/questions/72345439/nosuchelementexception-on-scanner-while-scanner-works-in-other-parts-of-code-sol#comment127807566_72345439) to your other post. TL;DR, fixing the syntax error won't make that code work anyway. – Federico klez Culloca May 23 '22 at 10:23
  • To delete a String, you need to read in the file, you don't do that in the code above, and then write to the file, skipping the String to delete. Your current code is missing a lot of steps. Note that there are many similar questions on deleting a specific String from a file, and you should look them up. – Hovercraft Full Of Eels May 23 '22 at 10:24
  • For example, [to delete a single line from a file](https://www.google.com/search?q=site%3Astackoverflow.com+java+delete+single+line+from+file) – Hovercraft Full Of Eels May 23 '22 at 10:40

0 Answers0