Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Sorted Linked List [modified]

Sorted Linked List [modified]

Scheduled Pinned Locked Moved Java
javadata-structureshelpquestion
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Babylon Lion
    wrote on last edited by
    #1

    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?! 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);
        }
    }
    

    }

    modified on Friday, November 26, 2010 10:59 PM

    T L L 3 Replies Last reply
    0
    • B Babylon Lion

      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?! 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);
          }
      }
      

      }

      modified on Friday, November 26, 2010 10:59 PM

      T Offline
      T Offline
      TorstenH
      wrote on last edited by
      #2

      Without copying the code into eclipse: No line found at java.util.Scanner.nextLine

      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);
          }
      }
      

      This method is the only position where "nextLine" is used. The 3. "nexLine" seems strange to me - you do not handle any replies there. So why do you read the next line? regards Torsten

      I never finish anyth...

      1 Reply Last reply
      0
      • B Babylon Lion

        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?! 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);
            }
        }
        

        }

        modified on Friday, November 26, 2010 10:59 PM

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        while (infile.hasNext())
        {
        fName = infile.nextLine();
        lName = infile.nextLine();
        id = infile.nextInt();
        infile.nextLine();

        ...
        

        }

        Your call to hasNext() only returns true if there is a line ready to be read, it does not guarantee that there will be three lines waitng, so your code may well fail as you see. BTW Please do not post your complete project in future, but just the lines relevant to the problem.

        Just say 'NO' to evaluated arguments for diadic functions! Ash

        T 1 Reply Last reply
        0
        • L Lost User

          while (infile.hasNext())
          {
          fName = infile.nextLine();
          lName = infile.nextLine();
          id = infile.nextInt();
          infile.nextLine();

          ...
          

          }

          Your call to hasNext() only returns true if there is a line ready to be read, it does not guarantee that there will be three lines waitng, so your code may well fail as you see. BTW Please do not post your complete project in future, but just the lines relevant to the problem.

          Just say 'NO' to evaluated arguments for diadic functions! Ash

          T Offline
          T Offline
          TorstenH
          wrote on last edited by
          #4

          just the lines relevant to the problem. ... if only he knew which lines are relevant... :rolleyes: regards Torsten

          I never finish anyth...

          L B 2 Replies Last reply
          0
          • T TorstenH

            just the lines relevant to the problem. ... if only he knew which lines are relevant... :rolleyes: regards Torsten

            I never finish anyth...

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            :)

            Just say 'NO' to evaluated arguments for diadic functions! Ash

            1 Reply Last reply
            0
            • B Babylon Lion

              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?! 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);
                  }
              }
              

              }

              modified on Friday, November 26, 2010 10:59 PM

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              Babylon Lion wrote:

              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)

              Why don't you take advantage of the information provided? a traceback shows exactly where the program is when the problem occurs. It is inside nextLine (not yours, so less relevant), called by createStudentList (yours) and to be more precise: in file mainStudnetProg.java at line 36. BTW: tell your IDE to always show line numbers in source file windows, and you're bound to see the problem pretty soon. :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

              Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

              B 1 Reply Last reply
              0
              • L Luc Pattyn

                Babylon Lion wrote:

                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)

                Why don't you take advantage of the information provided? a traceback shows exactly where the program is when the problem occurs. It is inside nextLine (not yours, so less relevant), called by createStudentList (yours) and to be more precise: in file mainStudnetProg.java at line 36. BTW: tell your IDE to always show line numbers in source file windows, and you're bound to see the problem pretty soon. :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                B Offline
                B Offline
                Babylon Lion
                wrote on last edited by
                #7

                Thank you all for the replies. I modified the code, and now it compiles ans run with no errors. But it doesn't output anything?! Any clue?

                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);
                    }
                }
                

                }

                L 1 Reply Last reply
                0
                • T TorstenH

                  just the lines relevant to the problem. ... if only he knew which lines are relevant... :rolleyes: regards Torsten

                  I never finish anyth...

                  B Offline
                  B Offline
                  Babylon Lion
                  wrote on last edited by
                  #8

                  I wasn't sure which lines were relevant but I knew I had a problem reading the file in main.

                  1 Reply Last reply
                  0
                  • B Babylon Lion

                    Thank you all for the replies. I modified the code, and now it compiles ans run with no errors. But it doesn't output anything?! Any clue?

                    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);
                        }
                    }
                    

                    }

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    Babylon Lion wrote:

                    it doesn't output anything?! Any clue?

                    If there are no run-time exceptions, then the only way to get some output is by calling a method that outputs something. Can you point us to some of those, as I don't see any. BTW: your hasNextLine() and nextLine() stuff is quite a mess IMO. May I suggest you reconsider your current code, clean it up, and then do one of these two things: 1. add logging statements that show where you are; a few System.out.println("Now going to ..."); could do wonders. 2. learn to use your debugger, with breakpoints and single-stepping, so you can slowly execute your code and look at the statements being executed one at a time. Also, if you're new to programming, maybe it would be wise: 1. to study an introductory book of your choice, from the start; 2. to start out with simpler stuff, probably from said book. Good luck.

                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                    Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups