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...

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

public class linkedList {

	// MAIN METHOD -----------------------------------//
	public static void main(String[] args) {
	
		linkedList myLinkedList = new linkedList();
		
		myLinkedList.displayList();
		myLinkedList.insert("vinny");
		myLinkedList.insert("darrell");
		myLinkedList.insert("mcdowell");
		myLinkedList.insert("zzz");
		myLinkedList.displayList();

		myLinkedList.printSection("abchijqsx");
		System.out.println("The first link is " + myLinkedList.head.next.value + ". The tail link is " + myLinkedList.tail.value + ".");
		
		myLinkedList.delete("darrell");
		myLinkedList.delete("vinny");
		myLinkedList.delete("zzz");
		
	}//-----------------------------------------------//


	DataInputStream in; 
	String fname, line, alphabet = "abcdefghijklmnopqrstuvwxyz";;
	boolean looping, searching;  	
	int linkCount = 0;
	link head = new link();
	link tail = new link();
	link temp = new link();
	link current = new link();
// 	link[] index = new link[26];
		
		
	public linkedList() {
		System.out.println("...starting linkedList() method...");
    	getFileName();
    	readFileContents();
	}
   
	public class link {
		public String value; // value of element
		public link next; // referene to next
		
		// constructor
		public link() {
		}
		public link(String val) {
			value = val;
		}
		public link(String val, link n) {
				value = val;
				next = n;
		}
	}
	
	
	/////////////////////////// PROMPT USER FOR .TXT FILE NAME ///////////////////
	public void getFileName() {
	   	Scanner in = new Scanner(System.in);
	   	System.out.println("What file do you want to turn into a linked list?");
			fname = in.nextLine();
	    	System.out.println("you entered: "+fname);
   }
	//////////////////////////////////////////////////////////////////////////////
	
	
	////// TRANSFER .TXT FILE'S CONTENTS INTO AN ALPHABETICALLY LINKED LIST //////
	public void readFileContents() {
	   	System.out.println("...starting readFileContents() method...");
	   	try {
				in = new DataInputStream(new FileInputStream(fname));
	    		looping = true;
				while(looping) {
	         	if (null == (line = in.readLine())) {
						looping = false;
	            	in.close();
	            }
					else {
						
						insert(line); // call insert method
						
					}// end else	
				}// end while
			}// end try
			catch(IOException e) {
				System.out.println("Error " + e);
			}// end catch
	}// end readFileContents()
	//////////////////////////////////////////////////////////////////////////////
	
	//////////////////////////////////// ALPHABETICALLY INSERT A LINK INTO THE LIST ///
	public void insert(String line) {
			link newLink = new link(line, null);
					temp = head;
					current = head.next;
					if (linkCount == 0) { // create first link
							head.next = newLink;
							current = newLink;
							temp = head;
							tail = newLink;
							searching = false;
					}
					else if (linkCount > 0) { // create additional link & insert
						searching = true;
						while (searching) {
							int compare = line.compareTo(current.value);
								if (current.next == null) {
									if (compare > 0) {
										current.next = newLink;
										temp = current;
										current = newLink;
										tail = newLink;
										searching = false;
									}
									if (compare < 0) {
										temp.next = newLink;
										temp = newLink;
										newLink.next = current;
										tail = current;
										searching = false;
									}
								}
								else if (current.next != null) {
									if (compare > 0) {
										temp = current;
										current = current.next;
									}
									if (compare < 0) {
										temp.next = newLink;
										newLink.next = current;
										temp = newLink;
										searching = false;
									}
								}
						}
					}
					linkCount++; // increment linkCount	
					System.out.println("'" + newLink.value + "' link was created and added to the list!");
					System.out.println("    linkCount is now: " + linkCount);
	}////////////////////////////////////////////////////////////////////////


	///////////////////////////////// DELETE A PARTICULAR LINK //////////////
	public void delete(String val) {
			current = head.next;
			temp = head;
			boolean looking = true;
			while (looking) {
				
				if (current.value == val) {
					temp.next = current.next;
					current = current.next;
					looking = false;
				}
				
				else if (current.value != val) {
					temp = current;
					current = current.next;
				}
			}
			
			
			linkCount--; // decrement linkCount
			System.out.println("'" + val + "' link was DELETED from the list!");
			System.out.println("    linkCount is now: " + linkCount);
	}/////////////////////////////////////////////////////////////////////////


	////////////////////////////// DISPLAY THE WHOLE LINKED LIST /////////////////////////////
	public void displayList() {
			String listString = new String("\ndisplayList() of all " + linkCount + " links : ");
			current = head.next;
				while (current.next != null) { //concatenate link values into listString
				listString = listString + current.value + ", ";
				current = current.next;
				}
			listString = listString + current.value + "\n";
			System.out.println(listString); //print listString	}
	}/////////////////////////////////////////////////////////////////////////////////////////


	/////////////////////////////////////////// PRINT SPECIFIC SECTIONS OF THE LINKED LIST ///
	public void printSection(String section) {
			int sectionCount = 0;
			String sectionString = new String();
				for (int i = 0; i < section.length(); i++) {	
					current = head.next;
					boolean printing = true;	
						while (printing) {
							if (current.value.charAt(0) != section.charAt(i) && current.next == null) {
								System.out.println("WARNING!!! NO STRING LINKS BEGINNING WITH '" + section.charAt(i) + "'!");
								printing = false;
							}
							
							else if (current.value.charAt(0) == section.charAt(i)) {	
								while (current.value.charAt(0) == section.charAt(i)) { //concatenate section values into sectionString
									sectionString = sectionString + current.value + ", ";
									sectionCount++; // increment sectionCount
									current = current.next;
								}
								printing = false;
							}
							else if (current.value.charAt(0) != section.charAt(i)) {
								current = current.next;
							}
							
						}
					if (current.next == null && current.value.charAt(0) == section.charAt(i)) {
							sectionString = sectionString + current.value;
					}
				}
			String output = new String("printSection(" + section + ") contains " + sectionCount + ": " + sectionString);
			System.out.println(output); // print sectionString
	}/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	

	 
}

bump...

Anyone bored and want something to do? :)
I'm sure it's some small syntax error somewhere...I just can't find it.

Comparing with the == Operator

http://java.sun.com/mailers/techtips/corejava/2006/tt0822.html

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.

public void delete(String val) {
        current = head.next;
        temp = head;
        boolean looking = true;
        while (looking) {
            if (current.value.equals(val)) {
                System.out.println("compare using '.equals(...)' method: " + (current.value.equals(val)));
                System.out.println("compare using '.compareTo(...)' method: " + (current.value.compareTo(val)));
                System.out.println("compare using '==' operator: " + (current.value == val));
                temp.next = current.next;
                current = current.next;
                looking = false;
            } else {
                temp = current;
                current = current.next;
            }
        }
        linkCount--; // decrement linkCount
        System.out.println("'" + val + "' link was DELETED from the list!");
        System.out.println("    linkCount is now: " + linkCount);
    }

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.