Practicing a lot of java and testing my codes until I become better at it.
Am reading records from a txt file and storing it into an array
import java.util.*; import java.io.*; public class PatientExercise { //patients exercise time public static void main (String[]args) throws IOException{ Scanner in = new Scanner(new FileReader("values.txt")); double [] patientTimeRecords = new double [300]; int noExerciseCount=0, numPatients =0; double highest=0, lowest=0, avg=0, totalTime=0; //iniitializing the array for(int i = 0; i < patientTimeRecords.length; i++) patientTimeRecords[i] = 0.0; int i=0; patientTimeRecords[i] = in.nextDouble(); //double []patientTime = double while(patientTimeRecords[i] >= 0) { totalTime = totalTime + patientTimeRecords[i]; //counts number of times patient didn't exercise if (patientTimeRecords[i] == 0) { noExerciseCount++; } //finds and updates highest exercise time if (patientTimeRecords[i] > highest) { highest=patientTimeRecords[i]; } //finds and updates lowest exercise time if (patientTimeRecords[i] < lowest) { lowest=patientTimeRecords[i]; } numPatients++; patientTimeRecords[i]= in.nextDouble(); }//end while avg =totalTime/numPatients; System.out.printf("Highest Time: %2.2f\n", highest); System.out.printf("Lowest Time: %2.2f\n", lowest); System.out.printf("Average Time: %2.2f\n", avg); System.out.printf("Number of times patient did not exeercise: %2.2f\n", noExerciseCount); } }
However an error msg keeps popping up:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextDouble(Scanner.java:2456)
at pastpapers.PatientExercise.main(PatientExercise.ja va:44)
line 44 is:patientTimeRecords[i]= in.nextDouble();