dimanche 26 avril 2015

Code skips if statement even though parameters are correct

This code is supposed to take the input from the customer in this format "$(amount of money)". The if statement is to check that the first character in the string payment equals a '$' but this doesn't set off the if statement it it keeps reading as an invalid payment.

double money = 0;
String payment = input.next();
String $ = "$";
String test = payment.substring(0);
if (test.equals($)) {
  System.out.println("You entered " + payment);
  payment = payment.substring(1, payment.length() - 1);
  money = Double.parseDouble(payment);
  if ( money < sum ) {
    System.out.println("Not enough money. System terminating.");
    System.exit(0);
  }
System.out.println(payment);
}
else {
  System.out.println("Invalid coin or note. Try again.");
  payment = input.next();
}

Aucun commentaire:

Enregistrer un commentaire