The code here worked until I needed to ensure the user didn't cause an exception by entering a string instead of an integer or double. I basically need to make sure the user enters enough to be greater than or equal to the price, so that the program can return the correct amount of change.
public static double findChange()
{
System.out.println("\nPlease insert: " + price + " (enter payment amount)");
while (payment < price)
{
try {
payment = kb.nextDouble();
//takes input until user has entered the needed amount
} catch (Exception e)
{
System.out.println("Error: Please enter valid currency");
}
price = price - payment;
price = (price * 100) / 100;
System.out.println("Please insert:" + price);
if (payment <= price)
stringError = false;
}
}
change = payment - price;
change = Math.round(change * 100);
change = change / 100;
System.out.println("\nChange Given: $" + change);
//determines amount of change to give user and formats to cents
return change;
}