Hey all,
I am trying to import a file of information into my java program and I am having a difficult problem with the loop to read the file.
The file I am importing has the following structure.
Name
number1
number2
number3
number4
number5
Name
number1
number2
number3
number4
number5
Name
.
.
.
You get the picture,
When the imput file has only one set of data (i.e, Nmae, number1number2, number3) the code is perfect. When I use a loop using the do (blah) while file.hasnext(). This works fine.
BUT BUT BUT.
When I add a second set of data I get an error when I import the file. The program compiles correctly but doesn'y read the second set of data. Is there an issue with changing the data ina string or somthing?
Please see my code below.
import java.util.*; import java.io.*; public class Marks { public static void main(String[] args) throws IOException { boolean nonExamHurdlePass; int numberOfFails = 0; String StudentName; double progressMark; int assignmentOneMark; double assignmentTwoMark; int labMark; int writtenMark; double totalMarks; double topMark = 0; double bottomMark =100; //ask user for file name System.out.println("Please entrer the name of the file containing the students marks details:"); Scanner kb = new Scanner(System.in); String filename = kb.nextLine(); File sourceFile = new File(filename); Scanner fileScanner = new Scanner(sourceFile); do { //reads the file StudentName = fileScanner.nextLine();; progressMark = fileScanner.nextDouble(); assignmentOneMark = fileScanner.nextInt(); assignmentTwoMark = fileScanner.nextDouble(); labMark = fileScanner.nextInt(); writtenMark = fileScanner.nextInt(); //calculate toal marks totalMarks = progressMark + assignmentOneMark + assignmentTwoMark + labMark + writtenMark; //Calculate top marks if(totalMarks > topMark) { topMark = totalMarks; } if(totalMarks < bottomMark) { bottomMark = totalMarks; } //System.out.println(totalMarks); //see if student passed non exam hurdle and store if((progressMark < 2.5 && assignmentOneMark < 5) && (assignmentTwoMark < 5 && labMark < 8)) { nonExamHurdlePass = true; } else { nonExamHurdlePass = false; } //calculate results for studnet if(totalMarks >= 50 && nonExamHurdlePass) { System.out.println(StudentName + " gets a Pass! Well Done"); } if(totalMarks < 50) { System.out.println(StudentName + " failed the course. Oh No."); numberOfFails++; } if(totalMarks >= 50 && !nonExamHurdlePass) { System.out.println(StudentName + " gets a SAH-A. Give assignment again"); } if(totalMarks >=50 && writtenMark <45) { System.out.println(StudentName + " gets a SAH-E. Give exam again"); } System.out.println(""); } while (fileScanner.hasNextLine()); System.out.println("The top mark in the class is " + topMark); System.out.println("The bottom mark in the class is " + bottomMark); System.out.println("The Number of Students that failed the course is " + numberOfFails); ///System.out.println(writtenMark); } }
Has anybody got any ideas? The error actually comes when running the program?
Thanks!
Also, yes this is for an assignment so if anybody out there is in my course please only use this as a guide and not copy it. That would only create headaches for everybody including yourself.