I am getting two errors from this program and I would like to know how I would f
ID: 3568980 • Letter: I
Question
I am getting two errors from this program and I would like to know how I would fix this. I've tried on my own and I just don't know what to change.
Here are the errors:
Reimbursement_3_09.java:28: error: incompatible types: possible lossy conversion from double to int
avgRe = (totalAmountgt0/ctrMiles);
^
Reimbursement_3_09.java:29: error: incompatible types: possible lossy conversion from double to int
avgMiles = (totalMilesgt0/ctrMiles);
Here is the program:
import java.io.*;
import java.util.*;
import java.text.*;
public class Reimbursement_3_09 {
static Toolkit tools = new Toolkit();
public static void main (String [] args) throws Exception {
Scanner input = new Scanner(System.in);
// Declare variables
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 = ("Reimbursement_3_09_Input.txt");
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("Reimbursement_3_09_Output.txt");
header (outFile);
details (outFile, miles, amounts, ctrMiles);
summary (outFile, totalAmount, ctrMiles, ctrMilesgt0, avgRe, avgMiles);
}// end main
//**************************************************************
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 < 400)
return .18*mileage;
else if (mileage < 900)
return 65.00 + (.15 * (mileage-400));
else if (mileage < 1300)
return 115.00 + (.12 * (mileage-900));
else if (mileage < 1900)
return 140.00 + (.10 * (mileage-1300));
else if (mileage < 2600)
return 165.00 + (.08 * (mileage-1900));
else if (mileage >= 2600)
return 195.00 + (.06 * (mileage-2600)); }
}
//***************************************************************
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;
}
}// End Class
Explanation / Answer
you, are declaring avRe , avgMiles as int . thats wrong.
int avgRe, avgMiles =0;
Instead you should decalre them double as below then you wont get error.
double avgRe, avgMiles =0;
==================================
Hope this helped!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.