Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Linked List 2
- * @author Mizuno, Anthony
- * @assignment ICS 211 Assignment 18
- * @date November 5, 2010
- */
- import javax.swing.JOptionPane;
- import java.util.*;
- import java.io.*;
- import java.lang.*;
- public class MizunoAnthony19
- {
- public static void main(String[] arguments)
- {
- try
- {
- /**
- * checks to see if there is exactly 1 commandline arguments
- */
- if(arguments.length != 1)
- {
- JOptionPane.showMessageDialog(null, "ERROR: Please enter a commandline argument.");
- }
- else
- {
- //create variables
- String temp1 = "";
- String temp2 = "";
- LinkedList<String> list = new LinkedList();
- File file = new File(arguments[0]);
- Scanner scan = null;
- scan = new Scanner(file).useDelimiter("\\s*,\\s*");
- while(scan.hasNext())
- {
- temp1 = scan.next();
- temp2 = scan.next();
- list.add(temp1,temp2);
- }
- JOptionPane.showMessageDialog(null,list.toString());
- } //end else
- } //end try
- /**
- * catches an input mismatch error from having wrong arguments
- */
- catch(InputMismatchException exception)
- {
- JOptionPane.showMessageDialog(null, "ERROR: Please enter arguments as the name of a csv file.");
- System.exit(1);
- } //end catch
- // catch exception if file not found
- catch (FileNotFoundException exception)
- {
- JOptionPane.showMessageDialog(null, "ERROR: File \"" + arguments[0] + "\" not found");
- }
- } //end main method
- } //end class MizunoAnthony18
- /**
- * PersonNode class
- * contains toString, getNext, and setNext methods
- */
- class PersonNode<T>
- {
- private T name;
- private T age;
- private PersonNode<T> next;
- /**
- * Creates a new PersonNode object
- * @param name2 name of person
- * @param age2 person's age
- */
- public PersonNode(T name2,T age2,PersonNode<T> next2)
- {
- name = name2;
- age = age2;
- next = next2;
- } //end constructor
- /**
- * Creates a printable version of the PersonNode object
- */
- public String toString()
- {
- String string;
- string = name + " is " + age + " years old.";
- return string;
- } //end toString
- public PersonNode<T> getNext()
- {
- return next;
- } //end getNext
- public void setNext(PersonNode<T> next2)
- {
- next = next2;
- } //end setNext
- } //end class PersonNode
- /**
- * LinkedList class
- *
- */
- class LinkedList<T>
- {
- private PersonNode<T> head = null;
- private int size = 0;
- public LinkedList()
- {
- }
- public void add(T name,T age)
- {
- if (head == null)
- {
- //if list empty, assign first item to new Node
- head = new PersonNode<T>(name,age,null);
- } //end if
- else
- {
- //if list isnt empty, add to end of list
- PersonNode<T> previous = head;
- PersonNode<T> current = head.getNext();
- //cycle through until reach last link
- while (current != null)
- {
- previous = current;
- current = current.getNext();
- }
- //once reach last link, make a new Node object
- PersonNode<T> node = new PersonNode<T>(name,age, null);
- //set new last link as new Node object
- previous.setNext(node);
- } //end else
- size++;
- } //end add method
- public String toString()
- {
- String string = "";
- // loop through nodes and add to string
- for (PersonNode<T> current = head.getNext(); current != null; current = current.getNext())
- {
- string = string + current.toString() + "\n";
- }
- return string;
- } //end toString
- } //end LinkedList class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement