Reputation: 3
For this assignment I was to write a code that would take a file (Numbers.txt) and calculate the mean and standard deviation of the numbers and print them out to a different file (Results.txt). My code compiled fine but when i go to enter Numbers.txt into my keyboard i get an error message.
Here is a snippet of my code
filename = keyboard.nextLine();
File file = new File(filename); //Create a FileReader object passing it the filename
Scanner inputFile = new Scanner(file);
line = inputFile.nextLine(); //priming read to read the first line of the file
while (inputFile.hasNextDouble()) //create a loop that continues until you are at the end of the file
{
sum += inputFile.nextDouble(); //convert the line into a double value and add the value to the sum
count ++; //increment the counter
inputFile.hasNext(); //read a new line from the file
}
inputFile.close(); //close the input file
mean = (sum/count); //store the calculated mean
File file2 = new File(filename); //Create a FileReader object passing it the filename
Scanner inputFile2 = new Scanner(file2); //reconnect to the FileReader object passing it the filename
//reconnect to the BufferedReader object passing it the FileReader object.
sum = 0; //reinitialize the sum of the numbers
count = 0; //reinitialize the number of numbers added
line = inputFile2.nextLine(); //priming read to read the first line of the file
while (inputFile2.hasNextDouble()) //loop that continues until you are at the end of the file
{
sum += inputFile2.nextDouble();
inputFile2.nextDouble();
}
difference = inputFile2.nextDouble() - mean; //convert the line into a double value and subtract the mean
sum += Math.pow(difference,2); //add the square of the difference to the sum
count ++; //increment the counter
if (inputFile2.hasNextDouble())
inputFile2.hasNext(); //read a new line from the file
inputFile2.close(); //close the input file
stdDev = Math.sqrt(sum/count); //store the calculated standard deviation
and here is the error message i get
java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at StatsDemo.main(StatsDemo.java:53)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
I'm sure I've made numerous errors with my coding, but i'm not experienced enough yet to notice them. Help would be greatly appreciated.
Finally, got the program to run and calculate the mean, but now I having a little difficulty figuring out how to use the nextDouble in conjunction with the subtraction of the mean. I believe i have to convert the line into a double value. Here's the part i'm having a bit trouble with:
File file2 = new File(filename); //Create a FileReader object passing it the filename
Scanner inputFile2 = new Scanner(file2); //reconnect to the FileReader object passing it the filename
//reconnect to the BufferedReader object passing it the FileReader object.
sum = 0; //reinitialize the sum of the numbers
count = 0; //reinitialize the number of numbers added
line = inputFile2.nextLine(); //priming read to read the first line of the file
while (inputFile2.hasNextDouble()) //loop that continues until you are at the end of the file
{
sum += inputFile2.nextDouble();
difference = inputFile2.nextDouble() - mean; //convert the line into a double value and subtract the mean
sum += Math.pow(difference,2); //add the square of the difference to the sum
count ++;
}//increment the counter
if (inputFile2.hasNextDouble())
{
inputFile2.hasNext(); //read a new line from the file
inputFile2.close(); //close the input file
stdDev = Math.sqrt(sum/count); //store the calculated standard deviation
}
Upvotes: 0
Views: 717
Reputation: 1320
After changing line 47 from line = inputFile.nextLine();
to line = inputFile2.nextLine();
(as suggested by user1598503), you now get a NoSuchElementException
on line 53.
It basically says your Scanner has no next double. Which is quite logical, because you have a while loop on line 48 which loops until it reaches the end of the file. Therefore, when you request the next double after the while loop, you are at the end of the file and there is no next double. The Java docs explain quite clearly why the NoSuchElementException
gets thrown.
You need to change your while-loop. Try to experiment more with it and/or find a good Java tutorial. I'm sure if you spend more time on it that you can make your program work.
Good luck!
Upvotes: 0
Reputation: 403
You closed the Scanner::inputFile and then used it again. As you can imagine, you can't use it if you closed it!
Upvotes: 1