Question: Please help in java code : output from code(below) is 617.25 but the answer it needs to be is: 617.2499999999999. What needs to be changed

Please help in java code : output from code(below) is 617.25 but the answer it needs to be is: 617.2499999999999. What needs to be changed so it stops rounding the decimal? thanks in advance!

with great effort some responded to change the code to this at the end System.out.println(String.format("%.15f", sum * 1.00 / numOfelement)); but, unfortunately that results in an answer of 617.250000000000000 .Does anyone have any other suggestions it would be greatly appreciated!

import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Scanner; public class six { public static void main(String[] args) throws FileNotFoundException { FileInputStream txtFile = new FileInputStream("Values.txt"); Scanner read = new Scanner(txtFile); double avg = 0; double numOfelement = 0, sum = 0 ; // Read Lines from values.txt while (read.hasNextLine()) { // Determine if line is integer try { double val = Double.parseDouble(read.nextLine()); double count = 0; // Determine if integer is prime for (double i = 2; i <= val / 2; i++) { if (val % i == 0) count++; } if (count == 0) { sum += val; numOfelement++; } // Keep running average of prime } catch (Exception e) { } } // Print Average of prime System.out.println(String.format("%.15f", sum * 1.00 / numOfelement)); read.close(); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!