Note that there are some explanatory texts on larger screens.

plurals
  1. PORounding error gives me invalid answer
    primarykey
    data
    text
    <p>I'm having a problem with rounding. For some reason I can't round <code>testP</code> to the tenth place. For example, on the first given example (Alex Smith) it gives me the answer of 82.0, but it should be 82.6. </p> <p>Here is the assignment that has the examples: <a href="http://www.hpcodewars.org/past/cw5/problems/Average.htm" rel="nofollow">http://www.hpcodewars.org/past/cw5/problems/Average.htm</a></p> <pre><code>import java.io.File; import java.io.FileOutputStream; import java.io.PrintWriter; import java.math.*; import java.util.Scanner; public class Average { public static void main(String[] args) { int num = 0, temp =0;; String fileName="AverageInput.txt"; String fileName2="AverageOutput.txt"; Scanner inputStream = null; PrintWriter outputStream = null; double homeworkP = 0; //read try{ inputStream = new Scanner(new File(fileName)); //try to open the file } catch(Exception e){ System.out.println("Could not open the file named "+ fileName); // if it doesn't find it, tell them System.exit(0); // and then exit. } //write try{ outputStream = new PrintWriter(new FileOutputStream(fileName2,true)); //try to create the file } catch(Exception e){ System.out.println("Could not open the file named "+ fileName2); // if it doesn't find it, tell them System.exit(0); // and then exit. } int numH = inputStream.nextInt(); int numT = inputStream.nextInt(); int[] arrayH = new int[numH]; outputStream.println("Averages"); String blank = inputStream.nextLine(); while (inputStream.hasNextLine()){ String line = inputStream.nextLine().replaceAll(" H","").replaceAll(" T",""); String[] nameParts = line.split(" "); String name = nameParts[0] +" "+ nameParts[1]; for (int i =0, p=2; i &lt; numH; i++, p++){ //collects homework grades. arrayH[i] = Integer.valueOf(nameParts[p]); temp = temp + arrayH[i]; } homeworkP = (temp - arrayMin(arrayH))/(numH-1) ; //gets percentage as rounded decimal. //System.out.println("homeP: " + homeworkP); temp =0; num = 0; for (int p = numH; p &lt; numT + numH; p++){ //collects test grades. num = num + Integer.valueOf(nameParts[p]); } double testP =num/numT; //gets percentage as decimal. System.out.println("homep: "+ homeworkP + "TestP: "+ testP); double TotalP = (homeworkP * 40) + (testP * 60); System.out.println(name + " " + TotalP); } outputStream.close(); inputStream.close(); } public static int arrayMin(int[] arr) { int i = 0; int min = Integer.MAX_VALUE; if (arr == null) { return 0; } else { while (i &lt; arr.length) { if (arr[i] &lt; min) { min = arr[i]; } i++; } } return min; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload