May I please have assistance with writing a JAVA program tha
May I please have assistance with writing a JAVA program that generates 10 random doubles, all between 1 and 11, and writes them to a text file, one number per line. Then I need to write another JAVA program 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
Solution
package chegg;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
public class WriteIntoFile {
public static void main(String args[])
{
String path = \"/Users/swapnil/Documents/input.txt\";
double r;
PrintWriter print_line;
FileWriter write;
try {
write = new FileWriter(path, false);
print_line = new PrintWriter(write);
Random randomGenerator = new Random();
for (int idx = 1; idx <= 10; ++idx){
r = 1 + randomGenerator.nextDouble()*10;
System.out.println(r);
print_line.printf(\"%f\" + \"%n\", r);
}
print_line.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/************** Read **********/
package chegg;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ReadFromFile {
public static void main(String[] args) {
String path = \"/Users/swapnil/Documents/input.txt\";
String line;
double sum = 0;
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
while ((line = br.readLine()) != null) {
System.out.println(line);
sum += Double.parseDouble(line.toString());
}
System.out.println(sum);
} catch (IOException e) {
e.printStackTrace();
}
}
}

