Sorted Linked List [modified]
-
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
-
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
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...
-
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
while (infile.hasNext())
{
fName = infile.nextLine();
lName = infile.nextLine();
id = infile.nextInt();
infile.nextLine();...
}
Your call to
hasNext()
only returnstrue
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
-
while (infile.hasNext())
{
fName = infile.nextLine();
lName = infile.nextLine();
id = infile.nextInt();
infile.nextLine();...
}
Your call to
hasNext()
only returnstrue
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
-
just the lines relevant to the problem. ... if only he knew which lines are relevant... :rolleyes: regards Torsten
I never finish anyth...
-
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
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.
-
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.
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); } }
}
-
just the lines relevant to the problem. ... if only he knew which lines are relevant... :rolleyes: regards Torsten
I never finish anyth...
I wasn't sure which lines were relevant but I knew I had a problem reading the file in main.
-
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); } }
}
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.