Hi, I need serious help with this code. So far everything compiles correctly but it does not output the correct change, and I've been trying for hours to figure out how to remedy the situation. All help is appreciated.

This is what is being inputed:
4
1.00 .01
100.00 .01
1.00 0.75
100.00 87.76

This is SUPPOSED to be the output:
Price: 1.0, paid: 0.01, change: 0.99. Yields: 10's: 0, 5's: 0, 1's: 0, Qtrs: 3, Dimes: 2, Nickels: 0, Pennies: 4
Price: 100.0, paid: 0.01, change: 99.99. Yields: 10's: 9, 5's: 1, 1's: 4, Qtrs: 3, Dimes: 2, Nickels: 0, Pennies: 4
Price: 1.0, paid: 0.75, change: 0.25. Yields: 10's: 0, 5's: 0, 1's: 0, Qtrs: 1, Dimes: 0, Nickels: 0, Pennies: 0
Price: 100.0, paid: 87.76, change: 12.239998. Yields: 10's: 1, 5's: 0, 1's: 2, Qtrs: 0, Dimes: 2, Nickels: 0, Pennies: 4
Checksum: 42


This is my algorithm so far:

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 = (int)(change / 1000);
				change%=1000;
				numFives = (int)(change / 500);
				change%=500;
				numOnes = (int)(change / 100);
				change%=100;
				numQuarters = (int)(change / 25);
				change%=25;
				numDimes = (int)(change / 10);
				change%=10;
				numNickels = (int)(change / 5);
				change%=5;
				numPennies = (int)(change / 1);
				change%=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
}

The flaw in your algorithm is that you are not multi[lying your change by 100. You need to multiply the change by 100 if you calculating the coins using Cent Value of the coin

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.