| | |
Deleting user specified lines from a text file
![]() |
I've got to make a Property To Let program that can allow the user to add, view or delete property details to and from a text file. I have done the add and view part but i am having trouble getting the delete working.
I've managed to get it to delete the last line of a text file by deleting a set amount of characters from the end. I've also been told by a friend that it involves removing all contents of that text file into a vector (yes i have to use vectors) and then deleting the specific line from there. but when i tried this it could never find the line in the file even though it was there.
My project has three relative classes, i'll display the code for these below. I'm also fairly new to java so forgive me if i need things in simple terms
I've managed to get it to delete the last line of a text file by deleting a set amount of characters from the end. I've also been told by a friend that it involves removing all contents of that text file into a vector (yes i have to use vectors) and then deleting the specific line from there. but when i tried this it could never find the line in the file even though it was there.
My project has three relative classes, i'll display the code for these below. I'm also fairly new to java so forgive me if i need things in simple terms
This is the main code
Java Syntax (Toggle Plain Text)
import java.io.*; import java.util.Vector; import java.io.File; import java.io.IOException; public class PropertyRental { public static Vector v = new Vector(); public static String menu; public static String[] addy; public static String pc, hn, mr, ua, readstr; public static int i, ln; public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public static rff Read = new rff(); public static void DataR() { readstr = Read.readFromText(); String[] splitArray = null; splitArray = readstr.split(","); for(int i=0;i<splitArray.length;i++) { if (v.size()<splitArray.length) { v.addElement(new PropertyToLet(splitArray[0], splitArray[1], splitArray[2])) ; } } } public static void main() { try { do { System.out.println("Welcome to your local Property Rental Company"); System.out.println("You now have the following options"); System.out.println(""); System.out.println("1. Add a Property"); System.out.println("2. Remove a Property"); System.out.println("3. Display all Properties"); System.out.println("4. Exit the Program"); System.out.println(""); i = Double.valueOf(br.readLine()).intValue(); System.out.println(""); if(i == 1) { //BufferedWriter appends to properties.txt //Our user input buffer BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("--- Add a Property ---"); //User enters address System.out.println("\nEnter postcode (must be written as pe158nb)"); pc = br.readLine(); //If the user has entered 'q' to quit, leave this loop System.out.println("Enter house number"); hn = br.readLine(); System.out.println("Enter monthly rent"); mr = br.readLine(); //Write our details to file v.addElement(new PropertyToLet(pc, hn, mr)); PrintWriter outFile; try { outFile = new PrintWriter(new FileWriter("properties.txt" ,true)); PropertyToLet obj = (PropertyToLet) (v.lastElement()); outFile.println(obj.getpc() + "," + obj.gethn() + "," + obj.getmr()); outFile.close(); } catch(IOException IOE) { System.err.println(IOE + "Has occured"); } } if(i == 2) { try { System.out.println("\nEnter the line number of the property you wish to remove"); int ln = Integer.valueOf(br.readLine()).intValue()-1; BufferedReader br=new BufferedReader(new FileReader("properties.txt")); String line=null; while ((line=br.readLine()) != null) { addy = line.split(","); PropertyToLet ptl=new PropertyToLet(addy[0],addy[1],addy[2]); v.addElement(ptl); } v.removeElementAt(ln); PrintWriter outFile = new PrintWriter(new FileWriter("properties.txt")); for (int i=0; i<v.size(); i++) { PropertyToLet obj = (PropertyToLet) (v.lastElement()); outFile.println(obj.getpc() + "," + obj.gethn() + "," + obj.getmr()); } outFile.close(); } catch(ArrayIndexOutOfBoundsException aioobe) { System.out.println("An Error Has occurred"); } catch(IOException ex) { ex.printStackTrace(); } } if(i == 3) { { String line=""; String data=""; try { BufferedReader br = new BufferedReader(new FileReader("properties.txt")); line = br.readLine(); int counter=0; do// EOF condition { counter++; System.out.println(counter + ": " + line); data = data + " \n" + line; //line = br.readLine(); } while( (line=br.readLine()) != null); } catch(IOException ioe) { } } } if(i == 4) { //A simple instruction that tells the user how to exit the program System.out.println("Press the 'x' button in the corner of the window to quit"); } System.out.println("Do you want to exit [y/n]"); ua = br.readLine(); }while(ua.equals ("n")); } catch(IOException ioe) { } } }
Last edited by Atomika3000; Apr 25th, 2009 at 3:11 pm. Reason: updated code since posting
This is the get functions
Java Syntax (Toggle Plain Text)
public class PropertyToLet { private String postCode; private String houseNumber; private String monthlyRent; public PropertyToLet (String pc, String hn, String mr) { postCode = pc; houseNumber = hn; monthlyRent = mr; } public String getpc() { return postCode; } public String gethn() { return houseNumber; } public String getmr() { return monthlyRent; } }
This is the reader code
Java Syntax (Toggle Plain Text)
import java.io.*; import java.util.*; public class rff { public static PropertyRental proprent = new PropertyRental(); public String readFromText() { String line=""; String data=""; try { BufferedReader br = new BufferedReader(new FileReader("properties.txt")); line = br.readLine(); do// EOF condition { //System.out.println(line); data = data + " \n" + line; //line = br.readLine(); } while( (line=br.readLine()) != null); } catch(IOException ioe) { } return data; } }
•
•
Join Date: Sep 2008
Posts: 1,563
Reputation:
Solved Threads: 196
To "delete" stuff from the text file, you have to read in the entire file, while deciding what you do and don't want in it, then write the output (the stuff you do want in the text file) back to the same file. You can do this one of two ways:
1. While you're reading it in, only add the stuff you do want to a list. Write the contents of that list out to the file.
2. Read everything into a list, then go through the list and delete the elements you don't want. Write out the list.
And it seems like your "delete" code is in (i==2) part of the code, right? You'll need to post an example text file also. We need to know what the format of the text file is in order to help you.
1. While you're reading it in, only add the stuff you do want to a list. Write the contents of that list out to the file.
2. Read everything into a list, then go through the list and delete the elements you don't want. Write out the list.
And it seems like your "delete" code is in (i==2) part of the code, right? You'll need to post an example text file also. We need to know what the format of the text file is in order to help you.
Yeah Sorry, Section 2 is the delete. the text file looks like this
pe158nb,68,500
pe158ul,1,1000
They have been written in as a vector.
any idea on ho0w i would go about doing what you suggested, as i said i'm new to java... and still have trouble piecing bits of code together?
pe158nb,68,500
pe158ul,1,1000
They have been written in as a vector.
any idea on ho0w i would go about doing what you suggested, as i said i'm new to java... and still have trouble piecing bits of code together?
Last edited by Atomika3000; Apr 25th, 2009 at 6:06 pm.
•
•
Join Date: Sep 2008
Posts: 1,563
Reputation:
Solved Threads: 196
Well, what are the user's options as far as deleting things? How would they specify what lines they want deleted? Does the "pe158nb" distinguish what "kind" of data is on the line? Or does the "nb"? This is information I need to know in order to help you. Basically -- how can the user delete stuff, and how does your program know what stuff to delete (i.e., the program will delete anything that says ub. . etc)?
![]() |
Similar Threads
- Help, Add and Delete many useres from text file (Shell Scripting)
- I NEED HELP PLEASE:Warning: mysql_num_rows(): (PHP)
- I NEED HELP PLEASE:Warning: mysql_num_rows(): (MySQL)
- Warning: mysql_num_rows(): (PHP)
- Help for Air Lines Reservition System (C)
- Aurora, DrPmon, MHTMLRedir problems (Viruses, Spyware and other Nasties)
- I know I have Something, but what? Please Help! (Viruses, Spyware and other Nasties)
Other Threads in the Java Forum
- Previous Thread: Running a program from Cmd Prmt in Vista
- Next Thread: NullPointerException: null (in sun.misc.Floating Decimal) Error?
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor crashcourse database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress integer intellij j2me java javadoc javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle plazmic print problem program programming project radio recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree unlimited utility webservices windows






