I am trying to delete a specific line from my text file using java, My code is supposed to write the file to a temp file and skip writing the username (chosen to be deleted), it is then supposed to delete the original file and rename the new temp file to match the original. The following is my code:
// remove a user
public static void deleteUser() throws FileNotFoundException, IOException {
System.out.println(
"Are you sure you want to delete a user? WARNING this will permanently remove them from our database\nmeaning you won't be able to recover their user from our index.txt file. (y/n)");
String answer = input.nextLine();
if (Objects.equals(answer, "y")) {
System.out.println("Please enter the exact username you would like to delete:");
String username = input.nextLine();
deleteUserFromFile(username, "index.txt", 1, " ");
System.out.println("User deleted.");
} else if (Objects.equals(answer, "n")) {
menu();
} else {
System.out.println("The answer provided can't be read. Please answer again using 'y' or 'n'.");
deleteUser();
}
}
// remove user function
public static void deleteUserFromFile(String username, String filepath, int positionOfTerm, String delimiter) {
String tempFile = "temp.txt";
File oldFile = new File(filepath);
File newFile = new File(tempFile);
String currentLine;
String data[];
try {
FileWriter fw = new FileWriter(tempFile, true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
FileReader fr = new FileReader(filepath);
BufferedReader br = new BufferedReader(fr);
while ((currentLine = br.readLine()) != null) {
data = currentLine.split(delimiter);
if (!(data[positionOfTerm].equalsIgnoreCase(username))) {
pw.println(currentLine);
}
}
pw.flush();
pw.close();
fr.close();
br.close();
bw.close();
fw.close();
oldFile.delete();
File dump = new File(filepath);
newFile.renameTo(dump);
} catch (Exception e) {
System.out.println("An error occured.");
}
}
The catch of this code is being triggered and I can't figure out why. Any help is appreciated.
For reference this is my index file:
6
0 Gromit
1 Gwendolyn
2 Le-Spiderman
3 Wallace
4 Batman
5 Superman
EDIT: I have included e.printStackTrace(); as suggested and this is the error I received from it:
java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
at Graph.deleteUserFromFile(Graph.java:239)
at Graph.deleteUser(Graph.java:208)
at Graph.menu(Graph.java:284)
at Graph.main(Graph.java:302)
The following is the referenced code, 302:
menu();
284:
deleteUser();
208 (username here is collected using a scanner input):
deleteUserFromFile(username, "index.txt", 1, " ");
239:
if (!(data[positionOfTerm].equalsIgnoreCase(username))) {
EDIT 2:
while ((currentLine = br.readLine()) != null) {
data = currentLine.split(delimiter);
String numOfUsernamesInList = br.readLine(); // updating the username number counter at the top of the
// index file
int newNumOfUsernamesInList = Integer.parseInt(numOfUsernamesInList) - 1;
pw.println(String.valueOf(newNumOfUsernamesInList));
if (data.length > positionOfTerm && !(data[positionOfTerm].equalsIgnoreCase(username))) {
pw.println(currentLine);
}
}
This is my attempt of getting the 6 shown in the index file to stay and update to a 5 when a username is deleted from the index file, the following is the error message:
java.lang.NumberFormatException: For input string: "0 Gromit"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.base/java.lang.Integer.parseInt(Integer.java:652)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at Graph.deleteUserFromFile(Graph.java:242)
at Graph.deleteUser(Graph.java:208)
at Graph.menu(Graph.java:290)
at Graph.main(Graph.java:308)
data[positionOfTerm]
on the first line of the file does not exist. You need to check the array length before.
if (data.length > positionOfTerm && !(data[positionOfTerm].equalsIgnoreCase(username))) {