Answered step by step
Verified Expert Solution
Link Copied!

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!

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(sum * 1.00 / numOfelement); read.close(); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Summarize training and development implementation issues.

Answered: 1 week ago