Question: I am trying to delete one line from just one line from the text file (if user enter username1 then delete all the data from

I am trying to delete one line from just one line from the text file

(if user enter username1 then delete all the data from the line include username1)

Can someone help me to fix my code?

Thanks

My code:

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

import java.io.*; import java.util.ArrayList; import java.util.Scanner;

public class DeleteAccount {

public static void main(String[] args) throws IOException { String filename = "example.txt"; Scanner s = new Scanner(new File(filename)); Scanner kb = new Scanner(System.in);

System.out.println("The Usename you want delete:"); String Username = kb.next();

ArrayList list = new ArrayList(); while (s.hasNext()) { //get it to array list list.add(s.next()); }

deleteFromFile(list, Username); } public static void deleteFromFile(ArrayList list, String search) throws IOException { String filename = "example.txt"; BufferedWriter writer = new BufferedWriter(new FileWriter(filename)); for (int i = 0; i < list.size(); i++) { if (list.get(i).equals(search)) { //remove twice because we have the account and password need to be remove together list.remove(i); list.remove(i); } } for(String str:list){ // output the String writer.write(str); writer.newLine(); } writer.close(); System.out.println(list.toString()); } }

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

My the data in my text file is look like :

Type1/User1/Password1/Street1/City1/Zipcode1 Type2/User2/Password2/Street2/City2/Zipcode2 Type3/User3/Password3/Street3/City3/Zipcode3 Type4/User4/Password4/Street4/City4/Zipcode4 Type5/User5/Password5/Street5/City5/Zipcode5 Type6/User6/Password6/Street6/City6/Zipcode6

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!