View Single Post
  #9 (permalink)  
Old 12-01-2009, 09:10 PM
bobbychiken bobbychiken is offline
Member
 
Join Date: Nov 2009
Posts: 8
Rep Power: 0
bobbychiken is on a distinguished road
Default
I'm having trouble with a last few compile errors that i didn't think would be too hard but I'm just not seeing where to fix it. and I've tried fiddling with double and int where it relates but I'm not getting anywhere.

these are the errors and the code is below

program9.java:21: possible loss of precision
found : double
required: int
avgRe = (totalAmountgt0/ctrMiles);
^
program9.java:22: possible loss of precision
found : double
required: int
avgMiles = (totalMilesgt0/ctrMiles);
^
2 errors

Code:
import java.io.*;
import java.util.*;
import java.text.*;

public class program9
{ public static void main (String [] args) throws Exception
 { double totalMiles, totalAmount , totalMilesgt0, totalAmountgt0 = 0; 
   int avgRe, avgMiles =0;
   double[]amounts = new double [10];
	double[]miles = new double [10];
	int ctrMiles = 0;
	int ctrMilesgt0 = 0;
   String filename = ("mg.dat");
	Scanner inFile = new Scanner(new FileReader(filename));
	ctrMiles = getMiles(inFile, ctrMiles, miles); 	  
	calcAmounts(miles, amounts, ctrMiles);  
	totalMiles = calcTotal(miles, ctrMiles);
	totalAmount = calcTotal(amounts, ctrMiles);
	totalMilesgt0 = calcTotalgt0(miles, ctrMiles);
	totalAmountgt0 = calcTotalgt0(amounts, ctrMiles);
	avgRe = (totalAmountgt0/ctrMiles);
	avgMiles = (totalMilesgt0/ctrMiles);
	PrintWriter outFile = new PrintWriter("mg.out");
	header (outFile);
	details (outFile, miles, amounts, ctrMiles);
	summary (outFile, totalAmount, ctrMiles, ctrMilesgt0, avgRe, avgMiles);
 }// end of body 
 //**************************************************************
 public static double calcTotal(double []number,int ctrMiles)
  {
   double temp = 0;
   for (int i = 0; i < ctrMiles; i++)
   	temp += number[i];
	return temp;	 
  }
//****************************************************************
 public static double calcTotalgt0(double []number,int ctrMiles)
  {
   int b = 0;
   for (int i = 0; i < ctrMiles; i++)
   { if(number[i] > 0)	
		b += number[i]; }
	return b;	 
  }
//****************************************************************
 public static int getMiles(Scanner inFile , int ctrMiles, double []miles)
 {
    int n = inFile.nextInt();
    for ( int i = 0; i < n; i++)
     miles[i] = inFile.nextDouble();
	 return n; 
 }
 //***************************************************************
 public static void details(PrintWriter outFile, double []miles, double []amounts,int numValues)	
 {	
     for(int i = 0;i < numValues;i++)
	  {
	  	if(miles[i] > 0)
		   outFile.println(leftpad1(miles[i],10) + " " + leftpad2(amounts[i],15));
      else outFile.println(leftpad1(miles[i],10) + " " + "          *****");
     }
 }	  
 //***************************************************************
  public static double calcMiles(double mileage)
  { 
    { if (mileage < 500)
		   return .15*mileage;  
      else if (mileage < 1000)
         return  75.0 + .12*(mileage - 500);
      else if (mileage < 1500)
         return 135.0 + .10*(mileage - 1000);
      else if (mileage < 2000)
         return 185.0 + .08*(mileage - 1500);
      else if (mileage < 3000)
         return 225.0 + .06*(mileage - 2000);
		else 
		return 285.0 + .05*(mileage - 3000);
	 }			
  }
//***************************************************************
public static void calcAmounts(double []miles, double []amounts, int numValues)
 { 
  for(int i = 0;i < numValues;i++)
   amounts[i] = calcMiles(miles[i]); 
 } 
//***************************************************************
 public static void header(PrintWriter outFile)
  { outFile.println	("     Report");
    outFile.println("      miles          amount");
  } 
 //***************************************************************
 public static void summary(PrintWriter outFile, double totalAmount, int ctrMiles, int ctrMilesgt0, int avgRe, int avgMiles)
 {
  outFile.println(" The total amount of money received for all the miles run is " + totalAmount + ".");
  outFile.println(" The number of mileage values that were processed was " + ctrMiles + ".");
  outFile.println(" The number of mileage values that were greater than zero are " + ctrMilesgt0 + ".");
  outFile.println(" The averge reimbursement is " + avgRe + ".");
  outFile.println(" The average miles ran are " + avgMiles + ".");
  outFile.close();
 } 
 //***************************************************************
 //   Returns miles formatted to one decimal place and padded by spaces
 //   on the left so that the String returned has length width       

 public static String leftpad1 (double miles, int width)
  {  String s;         // String to be returned
    int m;             //  length of s
    DecimalFormat fmt = new DecimalFormat("0.0");
    //  convert miles to a String with one decimal place
    s = fmt.format(miles);
    //  determine the length of s
    m = s.length();
    //  pad s by spaces on the left so that the resulting length of s is width
    for (int i = 0;  i < width - m;  i++)
          s = " " + s; // one space between the " " 
    return s;
  }
 //*******************************************************************
 //   Returns amount formatted to one decimal place and padded by spaces
 //   on the left so that the String returned has length width       

  public static String leftpad2 (double amount, int width)
  { String s;         // String to be returned
    int m;             //  length of s
    DecimalFormat fmt = new DecimalFormat("0.00");
    //  convert amount to a String with one decimal place
    s = fmt.format(amount);
    //  determine the length of s
    m = s.length();
    //  pad s by spaces on the left so that the resulting length of s is width
    for (int i = 0;  i < width - m;  i++)
      s = " " + s; // one space between the " "  
    return s;
  }
}
Reply With Quote