I've a problem to my linkedlist at line 10. Can anyone help me to solve it? Thank in advance.
This is my code :
package linkedlist;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class LinkedList
{
private String firstName;
private String lastName;
private static LinkedList<LinkedList> classRoster = new LinkedList<LinkedList>();
class C {
int i;
String s;
C(){
System.out.println("In main constructor");
// Other processing
}
C(int i){
this(i,"Blank");
System.out.println("In parameterized constructor 1");
}
C(int i, String s){
System.out.println("In parameterized constructor 2");
this.i = i;
this.s = s;
// Other processing
// Should this be a copy-paste from the main contructor?
// or is there any way to call it?
}
public void show(){
System.out.println("Show Method : " + i + ", "+ s);
}
}
private static class Node
{
LinkedList data;
private Node next = null;
private Node(LinkedList data, Node next)
{
this.data = data;
this.next = next;
}
private Node(LinkedList data)
{
this.data = data;
}
public LinkedList getData()
{
return data;
}
}
private static Node head;
/**
* Default Constructor
*/
public LinkedList()
{
head = null;
}
/**
* Constructor
* @param firstName First name of student
* @param lastName Last Name of Student
*/
public LinkedList(String firstName, String lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
public void addToStart(LinkedList data)
{
head = new Node(data, head);
}
/**
* Getter for firstName
* @return firstName
*/
public String getFirstName()
{
return firstName;
}
/**
* Setter for firstName
* @param firstName
*/
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
/**
* Getter for lastName
* @return lastName
*/
public String getLastName()
{
return lastName;
}
/**
* Setter for Last Name
* @param lastName
*/
public void setLastName(String lastName)
{
this.lastName = lastName;
}
/**
* toString method used for testing
*/
public String toString()
{
return "linkedList [firstName=" + firstName + ", lastName="
+ lastName + "]";
}
/**
* Prints out list to console
*/
public void printList()
{
int count = 0;
System.out.println("First Name: " + "Last Name: ");
while (count < classRoster.size())
{
LinkedList a = classRoster.get(count);
System.out.println(a.getFirstName() + " \t" + a.getLastName());
count++;
}
}
/**
*
* @param a linked list ran in
*/
public static void addToList(LinkedList a)
{
Node temp = head;
Node nextNode = null;
while (temp != null && nextNode.data.compareTo(a) >=0)
{
nextNode = temp;
temp = temp.next;
}
if (temp == null)
{
Node cur2 = new Node(a);
cur2.next = head;
head = cur2;
classRoster.add(cur2.getData());
}
else {
Node cur2 = new Node(a);
cur2.next = temp;
if (nextNode == null) {
nextNode.next = cur2;
classRoster.add(cur2.getData());
}
}
}
/**
* Reads in students names into object then adds and sorts list based on last name & firstName
* @param inputFileName The File Name
* @throws java.io.IOException
*/
public void read(String inputFileName) throws java.io.IOException
{
Scanner infile = new Scanner(new FileReader(inputFileName));
while(infile.hasNext())
{
String firstName = infile.next();
String lastName = infile.next();
LinkedList intoList = new LinkedList(firstName, lastName);
LinkedList.addToList(intoList);
}
infile.close();
}
public static void main(String[] args) throws IOException
{
LinkedList test = new LinkedList();
test.read("179ClassList.txt");
test.printList();
}
public int compareTo(LinkedList arg0)
{
if (this.lastName.equals(arg0.getLastName()))
return this.firstName.compareTo(arg0.getFirstName());
else
return this.lastName.compareTo(arg0.getLastName());
}
}