1 Replies - 488 Views - Last Post: 22 February 2010 - 07:52 PM Rate Topic: -----

#1 Guest_lorigami*


Reputation:

Linked List

Posted 22 February 2010 - 07:41 PM

Hi Guys,

I'm having trouble with my method findEmail, which is supposed to return a string of all the entries (names, phones, and emails) matching a given email address. I was wondering if I could get some help.

Thanks

import java.util.Scanner;

import javax.swing.JOptionPane;

import java.io.File;

import java.io.FileWriter;

import java.lang.Comparable;
//Main class
public class Main5 {
	//Main Method
	public static void main(String[] args) {
		// Creates a new PhoneBookLinkedList
		PhoneBookLinkedList<Entry> book = new PhoneBookLinkedList<Entry>();
		//Read the data from book
		readFromData(book, args[0]);
		//Save the file information to book
		saveToFile(book, args[0]);
		// Prints the info saved to book
		book.toPrint();
	}// End Main Method

	/* SaveToFile Method
	 * @param shopping list to be saved to the database 
	 */
	public static void saveToFile(PhoneBookLinkedList<Entry> entry,

	String databaseName) {

		try {
			//Creates a new file
			FileWriter out = new FileWriter(databaseName);
			//Writes the information to the database
			out.write(entry.toString());
			out.close();

		} catch (Exception e) {
			/* This is an error. Notify the user */
			String message = "unable to save to file " + databaseName;
			JOptionPane.showMessageDialog(null, message);
		}
	}// End SaveToFile

	/* readFromData Method
	 * @return shopping list read from a database, or empty shopping list
	 */
	public static PhoneBookLinkedList<Entry> readFromData(

	PhoneBookLinkedList<Entry> book, String dataname) {

		try {
			/* open the file for reading */
			File dbFile = new File(dataname);
			// creates a new instance of scanner
			Scanner dbRead = new Scanner(dbFile);
			// scans each line
			while (dbRead.hasNextLine()) {
				String name = dbRead.nextLine();
				String email = dbRead.nextLine();
				/* Long */String phoneNumber = dbRead.nextLine();
				book.add(new Entry(name, email, phoneNumber));
			}

		} catch (Exception e) {

			/*Assume the problem was due to the file not existing or
			 * having a bad format. Print a message to alert the user.
			 */
			System.out.println("error reading database file " + dataname);
		}
		return book;
	}//End readFromData
}//End Class

/*The Generic Node class*/
class Node<T> {

	// Data fields
	protected T data;
	protected Node<T> next;

	/*Constructor
	 * @param T dataItem
	 */
	public Node(T dataItem) {
		
		data = dataItem;
		next = null;
	}

	/*Constructor
	 * @param T dataItem
	 * @param Node<T> nextNode
	 */
	public Node(T dataItem, Node<T> nextNode) {
	
		data = dataItem;
		next = nextNode;
	}

	/*Gets the value of the next node
	 *@returns next 
	 */
	public Node<T> getNext() {
		
		return next;
	}

	/* Sets the value
	 * @param Node<T> newnext
	 */
	public void setNext(Node<T> newnext) {

		next = newnext;
	}

	/* Accessor that gets the value
	 * @returns data
	 */
	public T getValue() {

		return data;
	}

	/*getEmail Method
	 * @returns an e-mail address
	 */
	public String getEmail() {
		
		return null;
	}
}// End Node Class


class PhoneBookLinkedList<T extends Comparable<T>> {
	// Data Fields
	private Node<T> head = null;
	private int size = 0;

	/* Constructor */
	public PhoneBookLinkedList() {

	}

	/* Size Method
	 * @returns size
	 */
	public int size() {

		return size;
	}

	/* Add Method
	 *  @param T entry
	 */
	public void add(T entry) {

		// If the head is null then it creates a new node
		if (head == null) {
			// If the head is null then it creates a new node
			head = new Node<T>(entry, null);

		} else {

			// If there is already a non null node it finds where the new node
			// should be placed.
			Node<T> previous = head;
			
			while (previous.getNext() != null) {
				if (entry.compareTo(previous.getValue()) < 0) {
					break;
				}
				// gets the value of the next node
				previous = previous.getNext();
			}

			// creates a new node
			Node<T> newnode = new Node<T>(entry, previous.getNext());
			previous.setNext(newnode);
		}

		// increments
		size++;
	}

	// Removes a node from its position in the LinkedList
	public void remove(int position) {
		if (position == 1) {
			head = head.getNext();

		} else {

			// Sets the generic node to head
			Node<T> node = head;
			// Tests to see find the position in the list
			for (int i = 2; i < position; i++) {
				// gets the next node
				node = node.getNext();
			}
			node.setNext(node.getNext().getNext());
		}
		//decrements
		size--;
	}

	/*toString Method
	 * overrides java.lang.toString, returns the toString method
	 */
	public String toString() {
		return toString(head);
	}

	/*toString Method
	 * @param Node<T> node
	 */
	private String toString(Node<T> node) {
		if (node == null) {
			return "";
		} else {
			return node.getValue() + "\n" + toString(node.getNext());
		}
	}

	/*toPrint Method */
	public void toPrint() {
		for (Node<T> i = head; i != null; i = i.getNext()) {
			System.out.println(i.getValue().toString());
		}
	}

	/* findEmail Method
	 * @returns the email address
	 */
	public String findEmail(String email){
		for(Node<T> i = head; i != null; i = i.getNext()){
			boolean value = true;
			for(Integer j = 0; j < ((CharSequence) head).length(); j++){
				if(value == true){
					email = i.getEmail();
					if(head.substring(j,j+1).compareTo(email.substring(j,j+1)) == 0){
						value = true;
					}
					else{
						value = false;
					}
				}
				else{
					j = ((CharSequence) head).length();
				}
			}
			if(value == true){
				System.out.println(i.toString());
			}
		}
	}
}// End PhoneBook Class

class Entry implements Comparable<Entry> {

	// The data fields
	private String name;
	private String email;
	private String phoneNumber;

	/*Constructor 
	 * @param String n Variable for the name, type String
	 * @param String a Variable for the address, type String
	 * @param String e Variable for the e-mail, type String
	 */
	public Entry(String n, String e, /* Long */String ph) {

		name = n;
		email = e;
		phoneNumber = ph;

	}

	/* compareTo Method
	 * @param Entry e
	 */
	public int compareTo(Entry e) {
		return name.compareTo(e.name);
	}

	/* toString method 
	 * overrides java.lang.Object.toString
	 * @returns name, email, and phoneNumber
	 */
	public String toString() {

		return (name + "\n" + email + "\n" + phoneNumber);
	}
}// End Entry Class
 


Is This A Good Question/Topic? 0

Replies To: Linked List

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Linked List

Posted 22 February 2010 - 07:52 PM

Seems complicated for nothing to me
And your method returns nothing
You probably want something like that
       public String findEmail(String email){
          String answer = ""; 
          for(Node<T> i = head; i != null; i = i.getNext()){ 
              if(email.equals(i.getEmail()) {
                ... append to answer the user data
              } 
          } 
          return answer;
       }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1