Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.44 KB | None | 0 0
  1. /**
  2. * Linked List 2
  3. * @author Mizuno, Anthony
  4. * @assignment ICS 211 Assignment 18
  5. * @date November 5, 2010
  6. */
  7.  
  8. import javax.swing.JOptionPane;
  9. import java.util.*;
  10. import java.io.*;
  11. import java.lang.*;
  12.  
  13.  
  14. public class MizunoAnthony19
  15. {
  16.     public static void main(String[] arguments)
  17.     {
  18.         try
  19.         {
  20.             /**
  21.             * checks to see if there is exactly 1 commandline arguments
  22.             */
  23.             if(arguments.length != 1)
  24.             {
  25.                 JOptionPane.showMessageDialog(null, "ERROR: Please enter a commandline argument.");
  26.             }
  27.             else
  28.             {
  29.                 //create variables
  30.                 String temp1 = "";
  31.                 String temp2 = "";
  32.                 LinkedList<String> list = new LinkedList();
  33.                 File file = new File(arguments[0]);
  34.             Scanner scan = null;
  35.                
  36.                 scan = new Scanner(file).useDelimiter("\\s*,\\s*");
  37.                
  38.                 while(scan.hasNext())
  39.                 {
  40.                     temp1 = scan.next();
  41.                     temp2 = scan.next();
  42.                     list.add(temp1,temp2);
  43.                 }
  44.                            
  45.                 JOptionPane.showMessageDialog(null,list.toString());
  46.             } //end else
  47.         } //end try
  48.          
  49.         /**
  50.         * catches an input mismatch error from having wrong arguments
  51.         */
  52.         catch(InputMismatchException exception)
  53.         {
  54.             JOptionPane.showMessageDialog(null, "ERROR: Please enter arguments as the name of a csv file.");
  55.             System.exit(1);
  56.         } //end catch  
  57.         // catch exception if file not found
  58.       catch (FileNotFoundException exception)
  59.         {
  60.             JOptionPane.showMessageDialog(null, "ERROR: File \"" + arguments[0] + "\" not found");
  61.         }
  62.      
  63.     } //end main method  
  64. } //end class MizunoAnthony18
  65.  
  66.  
  67. /**
  68. * PersonNode class
  69. * contains toString, getNext, and setNext methods
  70. */
  71. class PersonNode<T>
  72. {
  73.     private T name;
  74.     private T age;
  75.     private PersonNode<T> next;
  76.     /**
  77.     * Creates a new PersonNode object
  78.     * @param name2 name of person
  79.     * @param age2 person's age
  80.     */
  81.     public PersonNode(T name2,T age2,PersonNode<T> next2)
  82.     {
  83.         name = name2;
  84.         age = age2;
  85.         next = next2;
  86.     } //end constructor
  87.  
  88.     /**
  89.     * Creates a printable version of the PersonNode object
  90.     */
  91.     public String toString()
  92.     {
  93.         String string;       
  94.         string = name + " is " + age + " years old.";        
  95.         return string;
  96.     } //end toString
  97.    
  98.     public PersonNode<T> getNext()
  99.     {
  100.         return next;
  101.     } //end getNext
  102.    
  103.     public void setNext(PersonNode<T> next2)
  104.     {
  105.         next = next2;
  106.     } //end setNext
  107.  
  108. } //end class PersonNode
  109.  
  110.  
  111. /**
  112. * LinkedList class
  113. *
  114. */
  115. class LinkedList<T>
  116. {
  117.     private PersonNode<T> head = null;
  118.     private int size = 0;
  119.    
  120.     public LinkedList()
  121.     {
  122.    
  123.     }
  124.    
  125.     public void add(T name,T age)
  126.     {
  127.    
  128.         if (head == null)
  129.         {
  130.             //if list empty, assign first item to new Node
  131.             head = new PersonNode<T>(name,age,null);
  132.         } //end if
  133.         else
  134.         {
  135.             //if list isnt empty, add to end of list
  136.             PersonNode<T> previous = head;
  137.             PersonNode<T> current = head.getNext();
  138.             //cycle through until reach last link
  139.             while (current != null)
  140.             {
  141.                 previous = current;
  142.                 current = current.getNext();
  143.             }
  144.             //once reach last link, make a new Node object
  145.             PersonNode<T> node = new PersonNode<T>(name,age, null);
  146.             //set new last link as new Node object
  147.             previous.setNext(node);
  148.         } //end else
  149.         size++;
  150.     } //end add method
  151.    
  152.     public String toString()
  153.     {
  154.         String string = "";
  155.         // loop through nodes and add to string
  156.         for (PersonNode<T> current = head.getNext(); current != null; current = current.getNext())
  157.         {
  158.             string = string + current.toString() + "\n";
  159.         }
  160.         return string;
  161.     } //end toString
  162.  
  163. } //end LinkedList class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement