dont know how to delete string..help!

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2008
Posts: 4
Reputation: Syntax Error is an unknown quantity at this point 
Solved Threads: 0
Syntax Error Syntax Error is offline Offline
Newbie Poster

dont know how to delete string..help!

 
0
  #1
Mar 17th, 2008
pls help!! can u give me some tips how to delete a string or list in a textfile based on this code....i created this code but i dont know how to delete it..pls explain in simple english because im not American..... tnx a lot...ur response is very much appreciated... =)

  1. import java.io.*;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.ObjectInputStream;
  5. import java.io.ObjectOutputStream;
  6.  
  7. public class TextFile
  8. {
  9. public static BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  10. public static String[] person;
  11. public static String[] tempPerson;
  12. public static String[] sepTemp;
  13. public static int xCtr;
  14.  
  15. public static void mainMenu()
  16. {
  17. System.out.println("1] Insert");
  18. System.out.println("2] Delete"); ////////////////
  19. System.out.println("3] Exit");///////////////
  20. }
  21. public static void insertData() throws IOException ///insert
  22. {
  23. System.out.print("Enter name: ");
  24. String pName = stdin.readLine();
  25. String temp;
  26.  
  27. if (xCtr != 0)
  28. {
  29.  
  30. temp =tempPerson[0] + ", " + pName;
  31.  
  32. }
  33. else
  34. {
  35. temp = pName;
  36. xCtr = 1;
  37. }
  38.  
  39. FileOutputStream x = new FileOutputStream("myTextFile.dat");
  40. ObjectOutputStream y = new ObjectOutputStream(x);
  41. y.writeObject(new String[] {temp});
  42.  
  43. System.out.println("New Entry Updated...");
  44.  
  45. y.close();
  46. }
  47.  
  48. public static void displayInitData() //displayInitial data
  49. {
  50. try
  51. {
  52.  
  53. FileInputStream x = new FileInputStream("myTextFile.dat");
  54. ObjectInputStream y = new ObjectInputStream(x);
  55. tempPerson = (String[])(y.readObject());
  56.  
  57. sepTemp = new String[100000];
  58. sepTemp = tempPerson[0].split(", ");
  59.  
  60.  
  61. int i = 0;
  62.  
  63. do
  64. {
  65. person[i] = sepTemp[i];
  66. i = i + 1;
  67. }
  68. while(i<sepTemp.length);
  69.  
  70.  
  71. xCtr = i;
  72.  
  73.  
  74. }
  75. catch(Exception m)
  76. {
  77. m.getMessage();
  78. }
  79.  
  80. }
  81.  
  82. public static void displayData() //display data
  83. {
  84. int i=0;
  85.  
  86. do
  87. {
  88. System.out.println(sepTemp[i]);
  89. i = i + 1;
  90. }
  91. while(i<sepTemp.length);
  92. }
  93. public static void main (String[] args) throws IOException, Exception
  94. {
  95. int choiceMenu;
  96.  
  97. person = new String[100000];
  98. tempPerson = new String[100000];
  99. displayInitData();
  100.  
  101. do
  102. {
  103. mainMenu();
  104. System.out.print("Enter Choice: ");
  105. choiceMenu = Integer.parseInt(stdin.readLine());
  106.  
  107. switch(choiceMenu)
  108. {
  109. case 1: displayInitData();
  110. insertData();
  111. break;
  112. case 2:delete();
  113. break;
  114. case 3:
  115. if (xCtr > 0)
  116. {
  117. displayInitData();
  118. displayData();
  119. }
  120. else
  121. System.out.println("No Record");
  122. break;
  123. default:
  124. }
  125. }
  126. while(choiceMenu!=3);
  127.  
  128. }
  129. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,424
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 258
Moderator
masijade's Avatar
masijade masijade is online now Online
Nearly a Posting Maven

Re: dont know how to delete string..help!

 
0
  #2
Mar 17th, 2008
Read the file and write a new file leaving out the pieces that should be "deleted". Then delete the original file and rename the newfile to the the name the oldfile had.
Java Programmer and Sun Systems Administrator

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

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 4
Reputation: Syntax Error is an unknown quantity at this point 
Solved Threads: 0
Syntax Error Syntax Error is offline Offline
Newbie Poster

Re: dont know how to delete string..help!

 
0
  #3
Mar 17th, 2008
hey thanks for the reply.......what about update? hehehe..just improving my program....tnx a lot.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,424
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 258
Moderator
masijade's Avatar
masijade masijade is online now Online
Nearly a Posting Maven

Re: dont know how to delete string..help!

 
0
  #4
Mar 17th, 2008
The same, of course, except that you modify what should be modified before writing it to the new file.
Java Programmer and Sun Systems Administrator

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

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
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