Deleting user specified lines from a text file

Reply

Join Date: Apr 2009
Posts: 12
Reputation: Atomika3000 is an unknown quantity at this point 
Solved Threads: 0
Atomika3000's Avatar
Atomika3000 Atomika3000 is offline Offline
Newbie Poster

Deleting user specified lines from a text file

 
0
  #1
Apr 25th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 12
Reputation: Atomika3000 is an unknown quantity at this point 
Solved Threads: 0
Atomika3000's Avatar
Atomika3000 Atomika3000 is offline Offline
Newbie Poster

Re: Deleting user specified lines from a text file

 
0
  #2
Apr 25th, 2009
This is the main code

  1. import java.io.*;
  2. import java.util.Vector;
  3. import java.io.File;
  4. import java.io.IOException;
  5.  
  6. public class PropertyRental
  7.  
  8. {
  9.  
  10. public static Vector v = new Vector();
  11. public static String menu;
  12. public static String[] addy;
  13. public static String pc, hn, mr, ua, readstr;
  14. public static int i, ln;
  15. public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  16. public static rff Read = new rff();
  17.  
  18. public static void DataR()
  19. {
  20. readstr = Read.readFromText();
  21. String[] splitArray = null;
  22. splitArray = readstr.split(",");
  23. for(int i=0;i<splitArray.length;i++)
  24. {
  25. if (v.size()<splitArray.length)
  26. {
  27. v.addElement(new PropertyToLet(splitArray[0], splitArray[1], splitArray[2])) ;
  28. }
  29. }
  30. }
  31.  
  32. public static void main()
  33.  
  34. {
  35.  
  36. try
  37. {
  38.  
  39. do
  40. {
  41. System.out.println("Welcome to your local Property Rental Company");
  42. System.out.println("You now have the following options");
  43. System.out.println("");
  44. System.out.println("1. Add a Property");
  45. System.out.println("2. Remove a Property");
  46. System.out.println("3. Display all Properties");
  47. System.out.println("4. Exit the Program");
  48. System.out.println("");
  49. i = Double.valueOf(br.readLine()).intValue();
  50. System.out.println("");
  51.  
  52. if(i == 1)
  53. {
  54. //BufferedWriter appends to properties.txt
  55. //Our user input buffer
  56. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  57. System.out.println("--- Add a Property ---");
  58.  
  59.  
  60.  
  61.  
  62.  
  63. //User enters address
  64. System.out.println("\nEnter postcode (must be written as pe158nb)");
  65. pc = br.readLine();
  66. //If the user has entered 'q' to quit, leave this loop
  67. System.out.println("Enter house number");
  68. hn = br.readLine();
  69. System.out.println("Enter monthly rent");
  70. mr = br.readLine();
  71. //Write our details to file
  72. v.addElement(new PropertyToLet(pc, hn, mr));
  73.  
  74. PrintWriter outFile;
  75. try
  76. {
  77. outFile = new PrintWriter(new FileWriter("properties.txt" ,true));
  78. PropertyToLet obj = (PropertyToLet) (v.lastElement());
  79. outFile.println(obj.getpc() + "," + obj.gethn() + "," + obj.getmr());
  80. outFile.close();
  81. }
  82.  
  83. catch(IOException IOE)
  84. {
  85. System.err.println(IOE + "Has occured");
  86. }
  87. }
  88.  
  89. if(i == 2)
  90. {
  91. try
  92.  
  93. {
  94. System.out.println("\nEnter the line number of the property you wish to remove");
  95. int ln = Integer.valueOf(br.readLine()).intValue()-1;
  96. BufferedReader br=new BufferedReader(new FileReader("properties.txt"));
  97. String line=null;
  98. while ((line=br.readLine()) != null)
  99. {
  100. addy = line.split(",");
  101. PropertyToLet ptl=new PropertyToLet(addy[0],addy[1],addy[2]);
  102. v.addElement(ptl);
  103. }
  104. v.removeElementAt(ln);
  105. PrintWriter outFile = new PrintWriter(new FileWriter("properties.txt"));
  106. for (int i=0; i<v.size(); i++)
  107. {
  108. PropertyToLet obj = (PropertyToLet) (v.lastElement());
  109. outFile.println(obj.getpc() + "," + obj.gethn() + "," + obj.getmr());
  110. }
  111. outFile.close();
  112. }
  113. catch(ArrayIndexOutOfBoundsException aioobe)
  114. {
  115. System.out.println("An Error Has occurred");
  116. }
  117. catch(IOException ex)
  118. {
  119. ex.printStackTrace();
  120. }
  121.  
  122. }
  123. if(i == 3)
  124. {
  125. {
  126. String line="";
  127. String data="";
  128. try
  129. {
  130. BufferedReader br = new BufferedReader(new FileReader("properties.txt"));
  131. line = br.readLine();
  132. int counter=0;
  133. do// EOF condition
  134. {
  135. counter++;
  136. System.out.println(counter + ": " + line);
  137. data = data + " \n" + line;
  138. //line = br.readLine();
  139. }
  140.  
  141. while( (line=br.readLine()) != null);
  142. }
  143. catch(IOException ioe)
  144. {
  145.  
  146. }
  147. }
  148. }
  149. if(i == 4)
  150. {
  151. //A simple instruction that tells the user how to exit the program
  152. System.out.println("Press the 'x' button in the corner of the window to quit");
  153. }
  154. System.out.println("Do you want to exit [y/n]");
  155. ua = br.readLine();
  156. }while(ua.equals ("n"));
  157. }
  158. catch(IOException ioe)
  159. {
  160.  
  161. }
  162.  
  163. }
  164.  
  165. }
Last edited by Atomika3000; Apr 25th, 2009 at 3:11 pm. Reason: updated code since posting
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 12
Reputation: Atomika3000 is an unknown quantity at this point 
Solved Threads: 0
Atomika3000's Avatar
Atomika3000 Atomika3000 is offline Offline
Newbie Poster

Re: Deleting user specified lines from a text file

 
0
  #3
Apr 25th, 2009
This is the get functions

  1. public class PropertyToLet
  2. {
  3. private String postCode;
  4. private String houseNumber;
  5. private String monthlyRent;
  6.  
  7. public PropertyToLet (String pc, String hn, String mr)
  8. {
  9. postCode = pc;
  10. houseNumber = hn;
  11. monthlyRent = mr;
  12. }
  13.  
  14. public String getpc()
  15. {
  16. return postCode;
  17. }
  18.  
  19. public String gethn()
  20. {
  21. return houseNumber;
  22. }
  23.  
  24. public String getmr()
  25. {
  26. return monthlyRent;
  27. }
  28.  
  29. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 12
Reputation: Atomika3000 is an unknown quantity at this point 
Solved Threads: 0
Atomika3000's Avatar
Atomika3000 Atomika3000 is offline Offline
Newbie Poster

Re: Deleting user specified lines from a text file

 
0
  #4
Apr 25th, 2009
This is the reader code

  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class rff
  5. {
  6.  
  7. public static PropertyRental proprent = new PropertyRental();
  8.  
  9.  
  10. public String readFromText()
  11. {
  12. String line="";
  13. String data="";
  14.  
  15. try
  16. {
  17. BufferedReader br = new BufferedReader(new FileReader("properties.txt"));
  18. line = br.readLine();
  19.  
  20.  
  21.  
  22. do// EOF condition
  23. {
  24. //System.out.println(line);
  25. data = data + " \n" + line;
  26. //line = br.readLine();
  27.  
  28. }
  29. while( (line=br.readLine()) != null);
  30. }
  31. catch(IOException ioe)
  32. {
  33.  
  34. }
  35. return data;
  36. }
  37. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,563
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Deleting user specified lines from a text file

 
0
  #5
Apr 25th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 12
Reputation: Atomika3000 is an unknown quantity at this point 
Solved Threads: 0
Atomika3000's Avatar
Atomika3000 Atomika3000 is offline Offline
Newbie Poster

Re: Deleting user specified lines from a text file

 
0
  #6
Apr 25th, 2009
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?
Last edited by Atomika3000; Apr 25th, 2009 at 6:06 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,563
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Deleting user specified lines from a text file

 
0
  #7
Apr 25th, 2009
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)?
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 12
Reputation: Atomika3000 is an unknown quantity at this point 
Solved Threads: 0
Atomika3000's Avatar
Atomika3000 Atomika3000 is offline Offline
Newbie Poster

Re: Deleting user specified lines from a text file

 
0
  #8
Apr 26th, 2009
in another section of the code (i == 3), they are assigned numbers, the user then types in that number on the delete section and it is then deleted from the file... well thats how it should work...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC