943,985 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1241
  • Java RSS
Oct 17th, 2009
1

Linked List program - stumped w/ 1 problem

Expand Post »
Hello...
This is my last assignment from my Data Structures class.
I turned it in and got a good grade, but it still has one problem that I am unable to pinpoint.

The assignment is as follows:
Create an alphabetical linked list from a .txt file containing various names. (1 per line)
Allow the addition/deletion of particular names, and also be able to show the number of names beginning with certain letters, print sections of names between particular letters, etc.

The problem I'm having is this:
It won't let me delete particular names that were read into the linked list on the initial .txt file read in. However, it WILL let me delete names that were added manually after the initial .txt file read in. This is really puzzling me because they both use the same delete method.

Like I said, I already turned this assignment in, but I really want to know what the cause for this error is!!

*I have also attached names.txt, which is the file that the program should read in. (it prompts you asking for file name to read)

Any help is appreciated!!
Here is the code...

Java Syntax (Toggle Plain Text)
  1.  
  2. import java.util.Scanner;
  3. import java.io.*;
  4.  
  5. public class linkedList {
  6.  
  7. // MAIN METHOD -----------------------------------//
  8. public static void main(String[] args) {
  9.  
  10. linkedList myLinkedList = new linkedList();
  11.  
  12. myLinkedList.displayList();
  13. myLinkedList.insert("vinny");
  14. myLinkedList.insert("darrell");
  15. myLinkedList.insert("mcdowell");
  16. myLinkedList.insert("zzz");
  17. myLinkedList.displayList();
  18.  
  19. myLinkedList.printSection("abchijqsx");
  20. System.out.println("The first link is " + myLinkedList.head.next.value + ". The tail link is " + myLinkedList.tail.value + ".");
  21.  
  22. myLinkedList.delete("darrell");
  23. myLinkedList.delete("vinny");
  24. myLinkedList.delete("zzz");
  25.  
  26. }//-----------------------------------------------//
  27.  
  28.  
  29. DataInputStream in;
  30. String fname, line, alphabet = "abcdefghijklmnopqrstuvwxyz";;
  31. boolean looping, searching;
  32. int linkCount = 0;
  33. link head = new link();
  34. link tail = new link();
  35. link temp = new link();
  36. link current = new link();
  37. // link[] index = new link[26];
  38.  
  39.  
  40. public linkedList() {
  41. System.out.println("...starting linkedList() method...");
  42. getFileName();
  43. readFileContents();
  44. }
  45.  
  46. public class link {
  47. public String value; // value of element
  48. public link next; // referene to next
  49.  
  50. // constructor
  51. public link() {
  52. }
  53. public link(String val) {
  54. value = val;
  55. }
  56. public link(String val, link n) {
  57. value = val;
  58. next = n;
  59. }
  60. }
  61.  
  62.  
  63. /////////////////////////// PROMPT USER FOR .TXT FILE NAME ///////////////////
  64. public void getFileName() {
  65. Scanner in = new Scanner(System.in);
  66. System.out.println("What file do you want to turn into a linked list?");
  67. fname = in.nextLine();
  68. System.out.println("you entered: "+fname);
  69. }
  70. //////////////////////////////////////////////////////////////////////////////
  71.  
  72.  
  73. ////// TRANSFER .TXT FILE'S CONTENTS INTO AN ALPHABETICALLY LINKED LIST //////
  74. public void readFileContents() {
  75. System.out.println("...starting readFileContents() method...");
  76. try {
  77. in = new DataInputStream(new FileInputStream(fname));
  78. looping = true;
  79. while(looping) {
  80. if (null == (line = in.readLine())) {
  81. looping = false;
  82. in.close();
  83. }
  84. else {
  85.  
  86. insert(line); // call insert method
  87.  
  88. }// end else
  89. }// end while
  90. }// end try
  91. catch(IOException e) {
  92. System.out.println("Error " + e);
  93. }// end catch
  94. }// end readFileContents()
  95. //////////////////////////////////////////////////////////////////////////////
  96.  
  97. //////////////////////////////////// ALPHABETICALLY INSERT A LINK INTO THE LIST ///
  98. public void insert(String line) {
  99. link newLink = new link(line, null);
  100. temp = head;
  101. current = head.next;
  102. if (linkCount == 0) { // create first link
  103. head.next = newLink;
  104. current = newLink;
  105. temp = head;
  106. tail = newLink;
  107. searching = false;
  108. }
  109. else if (linkCount > 0) { // create additional link & insert
  110. searching = true;
  111. while (searching) {
  112. int compare = line.compareTo(current.value);
  113. if (current.next == null) {
  114. if (compare > 0) {
  115. current.next = newLink;
  116. temp = current;
  117. current = newLink;
  118. tail = newLink;
  119. searching = false;
  120. }
  121. if (compare < 0) {
  122. temp.next = newLink;
  123. temp = newLink;
  124. newLink.next = current;
  125. tail = current;
  126. searching = false;
  127. }
  128. }
  129. else if (current.next != null) {
  130. if (compare > 0) {
  131. temp = current;
  132. current = current.next;
  133. }
  134. if (compare < 0) {
  135. temp.next = newLink;
  136. newLink.next = current;
  137. temp = newLink;
  138. searching = false;
  139. }
  140. }
  141. }
  142. }
  143. linkCount++; // increment linkCount
  144. System.out.println("'" + newLink.value + "' link was created and added to the list!");
  145. System.out.println(" linkCount is now: " + linkCount);
  146. }////////////////////////////////////////////////////////////////////////
  147.  
  148.  
  149. ///////////////////////////////// DELETE A PARTICULAR LINK //////////////
  150. public void delete(String val) {
  151. current = head.next;
  152. temp = head;
  153. boolean looking = true;
  154. while (looking) {
  155.  
  156. if (current.value == val) {
  157. temp.next = current.next;
  158. current = current.next;
  159. looking = false;
  160. }
  161.  
  162. else if (current.value != val) {
  163. temp = current;
  164. current = current.next;
  165. }
  166. }
  167.  
  168.  
  169. linkCount--; // decrement linkCount
  170. System.out.println("'" + val + "' link was DELETED from the list!");
  171. System.out.println(" linkCount is now: " + linkCount);
  172. }/////////////////////////////////////////////////////////////////////////
  173.  
  174.  
  175. ////////////////////////////// DISPLAY THE WHOLE LINKED LIST /////////////////////////////
  176. public void displayList() {
  177. String listString = new String("\ndisplayList() of all " + linkCount + " links : ");
  178. current = head.next;
  179. while (current.next != null) { //concatenate link values into listString
  180. listString = listString + current.value + ", ";
  181. current = current.next;
  182. }
  183. listString = listString + current.value + "\n";
  184. System.out.println(listString); //print listString }
  185. }/////////////////////////////////////////////////////////////////////////////////////////
  186.  
  187.  
  188. /////////////////////////////////////////// PRINT SPECIFIC SECTIONS OF THE LINKED LIST ///
  189. public void printSection(String section) {
  190. int sectionCount = 0;
  191. String sectionString = new String();
  192. for (int i = 0; i < section.length(); i++) {
  193. current = head.next;
  194. boolean printing = true;
  195. while (printing) {
  196. if (current.value.charAt(0) != section.charAt(i) && current.next == null) {
  197. System.out.println("WARNING!!! NO STRING LINKS BEGINNING WITH '" + section.charAt(i) + "'!");
  198. printing = false;
  199. }
  200.  
  201. else if (current.value.charAt(0) == section.charAt(i)) {
  202. while (current.value.charAt(0) == section.charAt(i)) { //concatenate section values into sectionString
  203. sectionString = sectionString + current.value + ", ";
  204. sectionCount++; // increment sectionCount
  205. current = current.next;
  206. }
  207. printing = false;
  208. }
  209. else if (current.value.charAt(0) != section.charAt(i)) {
  210. current = current.next;
  211. }
  212.  
  213. }
  214. if (current.next == null && current.value.charAt(0) == section.charAt(i)) {
  215. sectionString = sectionString + current.value;
  216. }
  217. }
  218. String output = new String("printSection(" + section + ") contains " + sectionCount + ": " + sectionString);
  219. System.out.println(output); // print sectionString
  220. }/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  221.  
  222.  
  223.  
  224. }
Attached Files
File Type: txt names.txt (529 Bytes, 26 views)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
nola_Coder is offline Offline
49 posts
since Oct 2009
Oct 19th, 2009
0
Re: Linked List program - stumped w/ 1 problem
bump...

Anyone bored and want something to do?
I'm sure it's some small syntax error somewhere...I just can't find it.
Last edited by nola_Coder; Oct 19th, 2009 at 7:15 pm.
Reputation Points: 10
Solved Threads: 0
Light Poster
nola_Coder is offline Offline
49 posts
since Oct 2009
Oct 20th, 2009
1

Comparing with the == Operator, comparing with the != Operator

Quote ...
Comparing with the == Operator
http://java.sun.com/mailers/techtips...06/tt0822.html
Quote ...
The == operator works on String object references. If two String variables point to the same object in memory, the comparison returns a true result. Otherwise, the comparison returns false, regardless whether the text has the same character values. The == operator does not compare actual char data. Without this clarification, you might be surprised that the following code snippet prints The strings are unequal.
Java Syntax (Toggle Plain Text)
  1. public void delete(String val) {
  2. current = head.next;
  3. temp = head;
  4. boolean looking = true;
  5. while (looking) {
  6. if (current.value.equals(val)) {
  7. System.out.println("compare using '.equals(...)' method: " + (current.value.equals(val)));
  8. System.out.println("compare using '.compareTo(...)' method: " + (current.value.compareTo(val)));
  9. System.out.println("compare using '==' operator: " + (current.value == val));
  10. temp.next = current.next;
  11. current = current.next;
  12. looking = false;
  13. } else {
  14. temp = current;
  15. current = current.next;
  16. }
  17. }
  18. linkCount--; // decrement linkCount
  19. System.out.println("'" + val + "' link was DELETED from the list!");
  20. System.out.println(" linkCount is now: " + linkCount);
  21. }
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Oct 21st, 2009
0
Re: Linked List program - stumped w/ 1 problem
Wow...it works.
Thank you!
Although it works, I don't really grasp why exactly, considering that the delete() method works for links that are added additionally, but not for those that were initially read into the linked list from the .txt file.
I'm sure that your post explained the reason behind this, but I'm not grasping the big picture just yet.
I'll look at it for another few minutes and think about it.

Thanks for the help and for the extra print statements showing the difference between those compare methods. Your efforts are appreciated!
Last edited by nola_Coder; Oct 21st, 2009 at 1:10 am.
Reputation Points: 10
Solved Threads: 0
Light Poster
nola_Coder is offline Offline
49 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Frequently Used Classes
Next Thread in Java Forum Timeline: Help!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC