Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started