I am trying to figure out why the sum at the end of my Java code is not working correctly. It keeps showing as zero and I do not know what the issue is. Here is the program requirements Write a program in a single file that: Main: Creates 10 random doubles, all between 1 and 11, Calls a method that writes 10 random doubles to a text file, one number per line. Calls a method that reads the text file and displays all the doubles and their sum accurate to two decimal places. SAMPLE OUTPUT 10.6269119604172 2.737790338909455 5.427925738865128 1.3742058065472509 1.1858700262498836 4.180391276485228 4.910969998930675 5.710858234343958 7.790857007373052 3.1806714736219543 The total is 47.13 Here is my code: import java.util.*; import java.io.*; public class AssignmentTwo { public static void main(String[] args) {//main method double randNum [] = new double [10]; for(int i = 0; i< randNum.length; i++) { randNum[i] = (double)(Math.random()* (10) + 1); System.out.println(randNum[i]); } writeNum(randNum); readFile(); } public static void writeNum(double num[]) {//write method try { File file = new File("numFile.txt"); PrintWriter output = new PrintWriter(file); for( int i = 0; i <= 9; i++) { output.print(num[i]+ "\n"); } } catch(Exception e) { System.out.println("Exception is " + e); } } public static void readFile(){//read method try { File file = new File("numFile.txt"); Scanner input = new Scanner(file); double sum= 0; while(input.hasNextDouble()) { double num = input.nextDouble(); System.out.println(num); sum = sum + num; } System.out.printf("\nThe total is %.2f", sum); } catch(Exception e) { System.out.println("Exception is " + e); } } }
I am trying to figure out why the sum at the end of my Java code is not working correctly. It keeps showing as zero and I do not know what the issue is. Here is the program requirements
Write a program in a single file that:
Main:
Creates 10 random doubles, all between 1 and 11,
Calls a method that writes 10 random doubles to a text file, one number per line.
Calls a method that reads the text file and displays all the doubles and their sum accurate to two decimal places.
SAMPLE OUTPUT
10.6269119604172
2.737790338909455
5.427925738865128
1.3742058065472509
1.1858700262498836
4.180391276485228
4.910969998930675
5.710858234343958
7.790857007373052
3.1806714736219543
The total is 47.13
Here is my code:
import java.util.*;
import java.io.*;
public class AssignmentTwo {
public static void main(String[] args) {//main method
double randNum [] = new double [10];
for(int i = 0; i< randNum.length; i++) {
randNum[i] = (double)(Math.random()* (10) + 1);
System.out.println(randNum[i]);
}
writeNum(randNum);
readFile();
}
public static void writeNum(double num[]) {//write method
try {
File file = new File("numFile.txt");
PrintWriter output = new PrintWriter(file);
for( int i = 0; i <= 9; i++) {
output.print(num[i]+ "\n");
}
}
catch(Exception e)
{
System.out.println("Exception is " + e);
}
}
public static void readFile(){//read method
try {
File file = new File("numFile.txt");
Scanner input = new Scanner(file);
double sum= 0;
while(input.hasNextDouble()) {
double num = input.nextDouble();
System.out.println(num);
sum = sum + num;
}
System.out.printf("\nThe total is %.2f", sum);
}
catch(Exception e)
{
System.out.println("Exception is " + e);
}
}
}
![](/static/compass_v2/shared-icons/check-mark.png)
the answer is given below:-
Step by step
Solved in 3 steps with 3 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)