This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

input is showing different than output  RSS feed

John Swain
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello, I have a program thatdetermines the change to be dispensed from a vending machine. it will display the change from 25 cents, 10 cents and 5 cents. I have all of the code completed but when i run the program the input price is different than what i typed in. I can not understand why it does this. Can anyone help or at least give me a hint on what im doing wrong? here is my code:public class changemaker25105
public static void main(String[] args)
throws {
int price, change, quarters, nickels, dimes;

System.out.println("Enter any number from 25 cents to a dollar, in increments of 5.");
System.out.println("I will display a combination of coins");
System.out.println("that equals amounts of change you will recieve.");

price =;
change = price;
System.out.println("You entered " + price);

quarters = price/25;
price = price%25;
dimes = price/10;
price = price%10;
nickels = price/5;

System.out.println(price + " cents in change can be given as:");

System.out.println(quarters + " quarters");
System.out.println(dimes + " dimes");
System.out.println(nickels + " nickels");
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that you misspelled "receive" -- "i before e except after c ... usually."

Okay, the real problem is that reads and returns a single character which will be an ASCII value between 48 and 57 if you type a 0 through 9 as the first character.

Instead, you want to read a whole line and parse it into an integer. A class I see a lot of students using is I just found it on Google -- I haven't looked at it. One simple way to do this would be with this (uncompiled/untested) code:I leave error-handling to you (exit or loop until you get a valid integer). The price variable is the one you declared in your code.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!