Hey guys, I need some help with this coding. I feel like it's something simple so hopefully someone can figure it out. I know for certain that it has to do with float-int conversion problems, and I feel like once I figure that out the rest should fall into place. The code calculates the change due to a costumer by using this input file here.
Here's the source:
package cecs174;
import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;
public class AssignmentFive
{
static final String FNAME = "ass5input.txt";
/**
* @param args
*/
public static void main(String[] args)
{
Scanner inFile = null;
try
{
inFile = new Scanner(new File(FNAME));
}
catch(FileNotFoundException e)
{
System.out.println("Error: " + e.getMessage());
System.out.println("Make sure " + FNAME + " is in the project's top level directory");
System.exit(1);
}
int count = inFile.nextInt();
int checkSum = 0;
for(int ii = 0; ii < count; ++ii)
{
float price = inFile.nextFloat();
float paid = inFile.nextFloat();
float change = price - paid;
float origChange = change;
System.out.print("Price: " + price + ", paid: " + paid + ", change: " + (price - paid) + ". Yields: ");
if(change <= 0) System.out.println("No change required");
else
{
int numTens = 0;
int numFives = 0;
int numOnes = 0;
int numQuarters = 0;
int numDimes = 0;
int numNickels = 0;
int numPennies = 0;
numTens = change / 1000;
change = change - (numTens * 1000);
numFives = change / 500;
change = change - (numFives * 500);
numOnes = change / 100;
change = change - (numOnes * 100);
numQuarters = change / 25;
change = change -(numQuarters * 25);
numDimes = change / 10;
change = change - (numDimes * 10);
numNickels = change / 5;
change = change - (numNickels * 5);
numPennies = change / 1;
change = change - (numPennies * 1);
System.out.println( "10's: " + numTens + ", " +
"5's: " + numFives + ", " +
"1's: " + numOnes + ", " +
"Qtrs: " + numQuarters + ", " +
"Dimes: " + numDimes + ", " +
"Nickels: " + numNickels + ", " +
"Pennies: " + numPennies);
if( numTens * 1000 +
numFives * 500 +
numOnes * 100 +
numQuarters * 25 +
numDimes * 10 +
numNickels * 5 +
numPennies * 1 != (origChange)
) System.out.println("***** ERROR *****");
checkSum += numTens + numFives + numOnes + numQuarters + numDimes
+ numNickels + numPennies;
} // end else
} // end for
System.out.println("Checksum: " + checkSum);
inFile.close();
} // end main
}