Sorted Linked list

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 2791 Views - Last Post: 28 November 2010 - 02:06 PM Rate Topic: -----

#1 asdbabil  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 241
  • Joined: 26-October 08

Sorted Linked list

Posted 25 November 2010 - 07:46 PM

hi all,
I'm working on a program that suppose to read data from a text file and then creates a linked list and outputs the data sorted by last name.
But I'm getting the following errors:
Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Unknown Source)
    at mainStudnetProg.createStudentList(mainStudnetProg.java:36)
    at mainStudnetProg.main(mainStudnetProg.java:15)


Where is the problem?!

this is how the data is stored in the text file
John 
Smith
1111
Kyle
Brown
2222
Steve
Jones
3333
Kelly
Walker
4444
Andrew 
Miller
5555



Please help.

main
import java.io.*;
import java.util.*;

public class mainStudnetProg
{
    public static void main(String[] args)
    {
        StudentList studentList = new StudentList();

        try
        {
            Scanner infile = new Scanner(new FileReader("C:\\studentInfo.txt"));

            createStudentList(infile, studentList);
        }
        catch (FileNotFoundException fnfe)
        {
            System.out.println(fnfe.toString());
        }
    } // end main

    public static void createStudentList(Scanner infile, StudentList studentList)
    {
        String  fName;
        String  lName;
        int id;

        Student newStudent;

        while (infile.hasNext())
        {
            fName = infile.nextLine();
            lName = infile.nextLine();
            id = infile.nextInt();
            infile.nextLine();
				
            newStudent = new Student();
            newStudent.setStudentInfo(fName, lName, id);
				
            studentList.insertFirst(newStudent);
        }
    }
}



StudentList

public class StudentList extends OrderedLinkedList<Student>
{
        //Default constructor
    public StudentList()
    {
        super();
    }

      //Method to print the info of all the videos in the
      //store.
    public void print()
    {
        LinkedListNode<Student> current; //variable to
                                        //traverse the list
        Student temp;

        current = first;    //set current so that it points to
                            //the first node
        while (current != null) //while more data to print
        {
            temp = (Student) current.info;
            temp.printStudentsInfo();
            current = current.link;
        }
    }
}



Student
public class Student implements Cloneable, Comparable
{
      //Instance Variables
    private String firstName;  
                      
    private String lastName;  
                                
    private int ID;  
                                

       //Default constructor
       //Instance variables are initialized to their
       //default values.
    public Student()
    {
        firstName = "";
        lastName = "";
        ID = 0;
    }

       //Constructor with parameters
       //Instance variables are set according to the parameters
    public Student(String fName, String lName, int id)
    {
        setStudentInfo(fName, lName, id);
    }

    public Object clone()
    {
        try
        {
            return super.clone();
        }
        catch (CloneNotSupportedException e)
        {
            return null;
        }
    }

    public void setStudentInfo(String fName, String lName,int id)
    {
        firstName = fName;
        lastName = lName;
        ID = id;
    }

       //Method to print the details 
    public void printStudentsInfo()
    {
        System.out.println("Student First Name: " + firstName);
        System.out.println("Student Last Name: " + lastName);
        System.out.println("Student ID: " + ID);
    }

    public String getLastName()
    {
        return lastName;
    }

    public boolean equals(Object otherStudent)
    {
        Student temp = (Student) otherStudent;

        return (lastName.compareTo(temp.lastName) == 0);
    }

    public int compareTo(Object otherStudent)
    {
        Student temp = (Student) otherStudent;

        return (lastName.compareTo(temp.lastName));
    }

    public String toString()
    {
        String studentInfo;

        studentInfo = "Student First Name: " + firstName + "\n"
                    + "Student Last Name: " + lastName + "\n"
                    + "Student ID: " + ID;

        return studentInfo;
    }
}



LinkedListADT
public interface LinkedListADT<T> extends Cloneable
{
    public Object clone();
       //Returns a copy of objects data in store.
       //This method clones only the references stored in
       //each node of the list. The objects that the
       //list nodes point to are not cloned.
    public boolean isEmptyList();
       //Method to determine whether the list is empty.
       //Postcondition: Returns true if the list is empty;
       //               false otherwise.
    public void initializeList();
       //Method to initialize the list to an empty state.
       //Postcondition: The list is initialized to an empty
       //               state.
    public void print();
       //Method to output the data contained in each node.
    public int length();
       //Method to return the number of nodes in the list.
       //Postcondition: The number of nodes in the list is
       //               returned.
    public T front();
       //Method to return a reference of the object containing
       //the data of the first node of the list.
       //Precondition: The list must exist and must not be empty.
       //Postcondition: The reference of the object that
       //               contains the info of the first node
       //               is returned.
    public T back();
       //Method to return a reference of object containing
       //the data of the last node of the list.
       //Precondition: The list must exist and must not be empty.
       //Postcondition: The reference of the object that
       //               contains the info of the last node
       //               is returned.
    public boolean search(T searchItem);
       //Method to determine whether searchItem is in the list.
       //Postcondition: Returns true if searchItem is found
       //               in the list; false otherwise.
    public void insertFirst(T newItem);
       //Method to insert newItem in the list.
       //Postcondition: newItem is inserted at the
       //               beginning of the list.
    public void insertLast(T newItem);
       //Method to insert newItem at the end of the list.
       //Postcondition: newItem is inserted at the end
       //               of the list.
    public void deleteNode(T deleteItem);
       //Method to delete deleteItem from the list.
       //Postcondition: If found, the node containing
       //               deleteItem is deleted from the
       //               list.
}



LinkedListClass

import java.util.NoSuchElementException;

public abstract class LinkedListClass<T> implements LinkedListADT<T>
{
    protected class LinkedListNode<T> implements Cloneable
    {
        public T info;
        public LinkedListNode<T> link;

           //Default constructor
           //Postcondition: info = null; link = null;
        public LinkedListNode()
        {
            info = null;
            link = null;
        }

           //Constructor with parameters
           //This method sets info pointing to the object to
           //which elem points to and link is set to point to
           //the object to which ptr points to.
           //Postcondition:  info = elem; link = ptr;
        public LinkedListNode(T elem, LinkedListNode<T> ptr)
        {
            info = elem;
            link = ptr;
        }

           //Returns a copy of objects data in store.
           //This method clones only the references stored in
           //the node. The objects that the nodes point to
           //are not cloned.
        public Object clone()
        {
            LinkedListNode<T> copy = null;

            try
            {
                copy = (LinkedListNode<T>) super.clone();

            }
            catch (CloneNotSupportedException e)
            {
                return null;
            }

            return copy;
        }

           //Method to return the info as a string.
           //Postcondition: info as a String object is
           //               returned.
        public String toString()
        {
            return info.toString();
        }
    } //end class LinkedListNode


    public class LinkedListIterator<T>
    {
        protected LinkedListNode<T> current;  //variable to
                                              //point to the
                                              //current node in
                                              //list
        protected LinkedListNode<T> previous; //variable to
                                              //point to the
                                              //node before the
                                              //current node

           //Default constructor
           //Sets current to point to the first node in the
           //list and sets previous to null.
           //Postcondition: current = first; previous = null;
        public LinkedListIterator()
        {
            current = (LinkedListNode<T>) first;
            previous = null;
        }

           //Method to reset the iterator to the first node
           //in the list.
           //Postcondition: current = first; previous = null;
        public void reset()
        {
            current = (LinkedListNode<T>) first;
            previous = null;
        }

           //Method to return a reference of the info of the
           //current node in the list and to advance iterator
           //to the next node.
           //Postcondition: previous = current;
           //               current = current.link;
           //               A refrence of the current node
           //               is returned.
        public T next()
        {
            if (!hasNext())
                throw new NoSuchElementException();

            LinkedListNode<T> temp = current;

            previous = current;
            current = current.link;

            return temp.info;
        }

            //Method to determine whether there is a next
            //element in the list.
            //Postcondition: Returns true if there is a next
            //               node in the list; otherwise
            //               returns false.
        public boolean hasNext()
        {
            return (current != null);
        }

           //Method to remove the node currently pointed to
           //by the iterator.
           //Postcondition: If iterator is not null, then the
           //               node that the iterator points to
           //               is removed. Otherwise the method
           //               throws NoSuchElementException.
        public void remove()
        {
            if (current == null)
                throw new NoSuchElementException();

            if (current == first)
            {
                first = first.link;
                current = (LinkedListNode<T>) first;
                previous = null;

                if (first == null)
                    last = null;
            }
            else
            {
                previous.link = current.link;

                if (current == last)
                {
                    last = first;
                    while (last.link != null)
                        last = last.link;
                }

                current = current.link;
            }

            count--;
        }

           //Method to return the info as a string.
           //Postcondition: info as a String object is returned.
        public String toString()
        {
            return current.info.toString();
        }

    } //end class LinkedListIterator


       //Instance variables of the class LinkedListClass
    protected LinkedListNode<T> first; //variable to store the
                                       //address of the first
                                       //node of the list
    protected LinkedListNode<T> last;  //variable to store the
                                       //address of the last
                                       //node of the list
    protected int count;  //variable to store the number of
                          //nodes in the list

       //Default constructor
       //Initializes the list to an empty state.
       //Postcondition: first = null, last = null,
       //               count = 0
    public LinkedListClass()
    {
        first = null;
        last = null;
        count = 0;
    }

       //Method to determine whether the list is empty.
       //Postcondition: Returns true if the list is empty;
       //               false otherwise.
    public boolean isEmptyList()
    {
        return (first == null);
    }

       //Method to initialize the list to an empty state.
       //Postcondition: first = null, last = null,
       //               count = 0
    public void initializeList()
    {
        first = null;
        last = null;
        count = 0;
    }

       //Method to output the data contained in each node.
    public void print()
    {
        LinkedListNode<T> current; //variable to traverse
                                   //the list

        current = first;    //set current so that it points to
                            //the first node
        while (current != null) //while more data to print
        {
            System.out.print(current.info + " ");
            current = current.link;
        }
    }//end print

       //Method to return the number of nodes in the list.
       //Postcondition: The value of count is returned.
    public int length()
    {
        return count;
    }

       //Method to return a reference of the object containing
       //the data of the first node of the list.
       //Precondition: The list must exist and must not be empty.
       //Postcondition: The reference of the object that
       //               contains the info of the first node
       //               is returned.
    public T front()
    {
        return first.info;
    }

        //Method to return a reference of object containing
        //the data of the last node of the list.
        //Precondition: The list must exist and must not be empty.
        //Postcondition: The reference of the object that
        //               contains the info of the last node
        //               is returned.
    public T back()
    {
        return last.info;
    }

       //Returns a copy of objects data in store.
       //This method clones only the references stored in
       //each node of the list. The objects that the
       //list nodes point to are not cloned.
    public Object clone()
    {
        LinkedListClass<T> copy = null;

        try
        {
            copy = (LinkedListClass<T>) super.clone();

        }
        catch (CloneNotSupportedException e)
        {
            return null;
        }

            //If the list is not empty clone each node of
            //the list.
        if (first != null)
        {
               //Clone the first node
            copy.first = (LinkedListNode<T>) first.clone();
            copy.last = copy.first;

            LinkedListNode<T> current;

            if (first != null)
                current = first.link;
            else
                current = null;

               //Clone the remaining nodes of the list
            while (current != null)
            {
                copy.last.link =
                        (LinkedListNode<T>) current.clone();
                copy.last = copy.last.link;
                current = current.link;
            }
        }

        return copy;
    }

       //Method to return an iterator of the list.
       //Postcondition: An iterator is instantiated and
       //               returned.
    public LinkedListIterator<T> iterator()
    {
        return new LinkedListIterator<T>();
    }

       //Method to determine whether searchItem is in
       //the list.
       //Postcondition: Returns true if searchItem is found
       //               in the list; false otherwise.
    public abstract boolean search(T searchItem);

       //Method to insert newItem in the list.
       //Postcondition: first points to the new list
       //               and newItem is inserted at the
       //               beginning of the list. Also,
       //               last points to the last node and
       //               count is incremented by 1.
    public abstract void insertFirst(T newItem);

       //Method to insert newItem at the end of the list.
       //Postcondition: first points to the new list and
       //               newItem is inserted at the end
       //               of the list. Also, last points to
       //               the last node and
       //               count is incremented by 1.
    public abstract void insertLast(T newItem);

       //Method to delete deleteItem from the list.
       //Postcondition: If found, the node containing
       //               deleteItem is deleted from the
       //               list. Also, first points to the first
       //               node, last points to the last
       //               node of the updated list, and count
       //               is decremented by 1.
    public abstract void deleteNode(T deleteItem);
}



OrderedLinkedList

public class OrderedLinkedList<T> extends LinkedListClass<T>
{
        //Default constructor
    public OrderedLinkedList()
    {
        super();
    }

        //Method to determine whether searchItem is in
        //the list.
        //Postcondition: Returns true if searchItem is found
        //               in the list; false otherwise.
    public boolean search(T searchItem)
    {
        LinkedListNode<T> current; //variable to traverse
                                   //the list
        boolean found;

        current = first;  //set current to point to the first
                          //node in the list

        found = false;    //set found to false

        while (current != null && !found ) //search the list
        {
            Comparable<T> temp = (Comparable<T>) current.info;

            if (temp.compareTo(searchItem) >= 0)
                found = true;
            else
                current = current.link; //make current point to
                                        //the next node
        }

        if (found)
           found = current.info.equals(searchItem);

        return found;
    }

        //Method to insert insertItem in the list.
        //Postcondition: first points to the new list,
        //       insertItem is inserted at the proper place
        //       in the list, and count is incremented by 1.
    public void insert(T insertItem)
    {
        LinkedListNode<T> current;  //variable to traverse
                                    //the list
        LinkedListNode<T> trailCurrent;    //variable just
                                           //before current
        LinkedListNode<T> newNode;  //variable to create a node

        boolean  found;

        newNode =
            new LinkedListNode<T>(insertItem, null); //create
                                                     //the node

        if (first == null)  //Case 1
        {
           first = newNode;
           last = newNode;
           count++;
        }
        else
        {
            trailCurrent = first;
            current = first;
            found = false;

            while (current != null && !found) //search the list
            {
                Comparable<T> temp =
                         (Comparable<T>) current.info;

                if (temp.compareTo(insertItem) >= 0)
                   found = true;
                else
                {
                   trailCurrent = current;
                   current = current.link;
                }
            }

            if (current == first)   //Case 2
            {
                newNode.link = first;
                first = newNode;
                count++;
            }
            else          //Case 3
            {
                trailCurrent.link = newNode;
                newNode.link = current;

                if (current == null)
                    last = newNode;

                count++;
            }
        }//end else
    }//end insert

        //Method to insert newItem in the list.
        //Because the resulting list is sorted, newItem is
        //inserted at the proper place.
        //This method uses the method insert to insert newItem.
        //Postcondition: first points to the new list
        //               and newItem is inserted at the
        //               proper place. Also,
        //               last points to the last node and
        //               count is incremented by 1.
    public void insertFirst(T newItem)
    {
        insert(newItem);
    }

        //Method to insert newItem in the list.
        //Because the resulting list is sorted, newItem is
        //inserted at the proper place.
        //This method uses the method insert to insert newItem.
        //Postcondition: first points to the new list
        //               and newItem is inserted at the
        //               proper place. Also,
        //               last points to the last node and
        //               count is incremented by 1.
    public void insertLast(T newItem)
    {
        insert(newItem);
    }//end insertLast

        //Method to delete deleteItem from the list.
        //Postcondition: If found, the node containing
        //               deleteItem is deleted from the
        //               list. Also, first points to the first
        //               node, last points to the last
        //               node of the updated list, and count
        //               is decremented by 1.
    public void deleteNode(T deleteItem)
    {
        LinkedListNode<T> current;      //variable to traverse
                                        //the list
        LinkedListNode<T> trailCurrent; //variable just before
                                        //current
        boolean found;

        if (first == null)    //Case 1; list is empty.
            System.err.println("Cannot delete from an "
                             + "empty list.");
        else
        {
            if (first.info.equals(deleteItem)) //Case 2
            {
                first = first.link;

                if (first == null)
                    last = null;
                count--;
            }
            else  //search the list for the node with
                  //the given info
            {
                found = false;
                trailCurrent = first; //set trailCurrent to
                                      //point to the first node
                current = first.link; //set current to point
                                      //to the second node

                while (current != null && !found)
                {
                    Comparable<T> temp =
                             (Comparable<T>) current.info;

                    if (temp.compareTo(deleteItem) >= 0)
                        found = true;
                    else
                    {
                        trailCurrent = current;
                        current = current.link;
                    }
                }//end while

                if (current == null)   //Case 4
                    System.out.println("Item to be deleted is "
                                     + "not in the list.");
                else
                    if (current.info.equals(deleteItem)) //item
                                 //to be deleted is in the list
                    {
                        if (first == current)    //Case 2
                        {
                            first = first.link;

                            if (first == null)
                                last = null;

                            count--;
                        }
                        else                    //Case 3
                        {
                            trailCurrent.link = current.link;

                            if (current == last)
                                last = trailCurrent;

                            count--;
                        }
                    }
                    else                   //Case 4
                        System.out.println("Item to be deleted "
                                         + "is not in the list.");
            }
        }//end else
    }//end deleteNode
}


This post has been edited by asdbabil: 25 November 2010 - 08:04 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Sorted Linked list

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: Sorted Linked list

Posted 25 November 2010 - 07:55 PM

I hope that the &gt; and &lt; things are just typos because in order to use generics with things like LinkedLists AND using generics period, you need to use the < and > characters...No need to use the HTML escape characters.

Object<Generic>



NOT

Object&lt;Generic&gt;


Was This Post Helpful? 1
  • +
  • -

#3 asdbabil  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 241
  • Joined: 26-October 08

Re: Sorted Linked list

Posted 25 November 2010 - 08:00 PM

View PostDogstopper, on 25 November 2010 - 08:55 PM, said:

I hope that the &gt; and &lt; things are just typos because in order to use generics with things like LinkedLists AND using generics period, you need to use the < and > characters...No need to use the HTML escape characters.

Object<Generic>



NOT

Object&lt;Generic&gt;



yeah never mind those characters..the original code doesn't have those and still get the same errors :D
Was This Post Helpful? 0
  • +
  • -

#4 asdbabil  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 241
  • Joined: 26-October 08

Re: Sorted Linked list

Posted 25 November 2010 - 08:06 PM

fixed
Was This Post Helpful? 0
  • +
  • -

#5 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: Sorted Linked list

Posted 25 November 2010 - 08:07 PM

Ok, does it help if you make these changes?
        while (infile.hasNextLine())
        {
            fName = infile.nextLine();
 
            // If there's no line, then break out of the loop.
            if (!infile.hasNextLine()) 
                break;
            lName = infile.nextLine();
            id = infile.nextInt();
            infile.nextLine();
				
            newStudent = new Student();
            newStudent.setStudentInfo(fName, lName, id);
				
            studentList.insertFirst(newStudent);
        }


Was This Post Helpful? 0
  • +
  • -

#6 asdbabil  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 241
  • Joined: 26-October 08

Re: Sorted Linked list

Posted 25 November 2010 - 08:13 PM

View PostDogstopper, on 25 November 2010 - 09:07 PM, said:

Ok, does it help if you make these changes?
        while (infile.hasNextLine())
        {
            fName = infile.nextLine();
 
            // If there's no line, then break out of the loop.
            if (!infile.hasNextLine()) 
                break;
            lName = infile.nextLine();
            id = infile.nextInt();
            infile.nextLine();
				
            newStudent = new Student();
            newStudent.setStudentInfo(fName, lName, id);
				
            studentList.insertFirst(newStudent);
        }



I replaced my while loop with yours and I got this
Exception in thread "main" java.util.NoSuchElementException: No line found
	at java.util.Scanner.nextLine(Unknown Source)
	at mainStudnetProg.createStudentList(mainStudnetProg.java:39)
	at mainStudnetProg.main(mainStudnetProg.java:15)


Was This Post Helpful? 0
  • +
  • -

#7 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: Sorted Linked list

Posted 25 November 2010 - 08:20 PM

Oops. I missed one. With a Scanner, before EVERY call to next*(), make sure you check that they exist using the hasNext*() method(s). That means that before EACH CALL to nextLine() or nextInt(), you need to make a call to hasNextLine() or hasNextInt().
Was This Post Helpful? 0
  • +
  • -

#8 asdbabil  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 241
  • Joined: 26-October 08

Re: Sorted Linked list

Posted 25 November 2010 - 08:27 PM

View PostDogstopper, on 25 November 2010 - 09:20 PM, said:

Oops. I missed one. With a Scanner, before EVERY call to next*(), make sure you check that they exist using the hasNext*() method(s). That means that before EACH CALL to nextLine() or nextInt(), you need to make a call to hasNextLine() or hasNextInt().


you mean like this:
.
.
infile.hasNextLine();
fName = infile.nextLine();
.
.
infile.hasNextLine();
fName = infile.nextLine();

infile.hasNextint();
id = infile.nextInt();

infile.hasNextLine();
infile.nextLine();



if that what you meant, it didn't work..errors:
xception in thread "main" java.util.NoSuchElementException: No line found
	at java.util.Scanner.nextLine(Unknown Source)
	at mainStudnetProg.createStudentList(mainStudnetProg.java:44)
	at mainStudnetProg.main(mainStudnetProg.java:15)



This post has been edited by asdbabil: 25 November 2010 - 08:36 PM

Was This Post Helpful? 0
  • +
  • -

#9 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: Sorted Linked list

Posted 25 November 2010 - 08:38 PM

Well, you need to couple the has**() methods with if statements so that if there ISN'T anything to read, we stop. Your last code was close, but not quite. All you need to do is wrap an if statement around it.
Was This Post Helpful? 1
  • +
  • -

#10 asdbabil  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 241
  • Joined: 26-October 08

Re: Sorted Linked list

Posted 25 November 2010 - 08:52 PM

View PostDogstopper, on 25 November 2010 - 09:38 PM, said:

Well, you need to couple the has**() methods with if statements so that if there ISN'T anything to read, we stop. Your last code was close, but not quite. All you need to do is wrap an if statement around it.


I ran it and I got not errors but there's nothing in the console!!
here's how I did the code:
if (!infile.hasNextLine()){
        		break;
        	}else{
        		infile.hasNextLine();
        		fName = infile.nextLine();	
        	}
        	
            // If there's no line, then break out of the loop.
            if (!infile.hasNextLine()) 
                break;
            if (!infile.hasNextLine()){
            	break;
            }else{
                infile.hasNextLine();
                lName = infile.nextLine();
            }

            if (!infile.hasNextInt()){
            	break;
            }else{
                infile.hasNextInt();
                id = infile.nextInt();
            }
            
            if (!infile.hasNextLine()){
            	break;
            }else{
                infile.hasNextLine();
                infile.nextLine();
            }


Was This Post Helpful? 0
  • +
  • -

#11 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: Sorted Linked list

Posted 25 November 2010 - 09:02 PM

You can simplify your code a lot!

            if (!infile.hasNextLine()){
        		break;
            }
            fName = infile.nextLine();	
        
        	
            // If there's no line, then break out of the loop.
            if (!infile.hasNextLine()){
            	break;
            }
            lName = infile.nextLine();

            if (!infile.hasNextInt()){
            	break;
            }
            id = infile.nextInt();
            
            if (!infile.hasNextLine()){
            	break;
            }
            infile.nextLine();



And if nothing is showing up, that means that you are reading nothing. Perhaps your file isn't loading correctly?
Was This Post Helpful? 1
  • +
  • -

#12 asdbabil  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 241
  • Joined: 26-October 08

Re: Sorted Linked list

Posted 25 November 2010 - 10:19 PM

View PostDogstopper, on 25 November 2010 - 10:02 PM, said:

You can simplify your code a lot!

            if (!infile.hasNextLine()){
        		break;
            }
            fName = infile.nextLine();	
        
        	
            // If there's no line, then break out of the loop.
            if (!infile.hasNextLine()){
            	break;
            }
            lName = infile.nextLine();

            if (!infile.hasNextInt()){
            	break;
            }
            id = infile.nextInt();
            
            if (!infile.hasNextLine()){
            	break;
            }
            infile.nextLine();



And if nothing is showing up, that means that you are reading nothing. Perhaps your file isn't loading correctly?



but there's data in the file!...how can I make it load correctly then?
BUILD SUCCESSFUL (total time: 0 seconds)


Was This Post Helpful? 0
  • +
  • -

#13 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: Sorted Linked list

Posted 26 November 2010 - 07:52 AM

The original code you posted is rather lengthy. Could you post the part where you open the File from which you use the scanner to read?
Was This Post Helpful? 0
  • +
  • -

#14 asdbabil  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 241
  • Joined: 26-October 08

Re: Sorted Linked list

Posted 26 November 2010 - 07:49 PM

View PostDogstopper, on 26 November 2010 - 08:52 AM, said:

The original code you posted is rather lengthy. Could you post the part where you open the File from which you use the scanner to read?


you mean this code?

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

public class mainStudnetProg
{
    public static void main(String[] args)
    {
        StudentList studentList = new StudentList();

        try
        {
            Scanner infile = new Scanner(new FileReader("C:\\studentInfo.txt"));

            createStudentList(infile, studentList);
        }
        catch (FileNotFoundException fnfe)
        {
            System.out.println(fnfe.toString());
        }
    } // end main

    public static void createStudentList(Scanner infile, StudentList studentList)
    {
        String  fName;
        String  lName;
        int id;

        Student newStudent;
        while (infile.hasNextLine())
        {
        	if (!infile.hasNextLine()){
        		break;
        	}
    		infile.hasNextLine();
    		fName = infile.nextLine();	
 
        	
            // If there's no line, then break out of the loop.
            if (!infile.hasNextLine()){
            	break;
            }
            infile.hasNextLine();
            lName = infile.nextLine();

            if (!infile.hasNextInt()){
            	break;
            }
            infile.hasNextInt();
            id = infile.nextInt();
            
            if (!infile.hasNextLine()){
            	break;
            }
            infile.hasNextLine();
            infile.nextLine();
            
            newStudent = new Student();
            newStudent.setStudentInfo(fName, lName, id);

            studentList.insertFirst(newStudent);
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#15 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: Sorted Linked list

Posted 26 November 2010 - 09:06 PM

I am too tired right now to notice anything wrong. I need more coffee Java. But what I suggest is put some System.out statements in there for debugging purposes.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2