Forums Register Login

input is showing different than output

+Pie Number of slices to send: Send
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 java.io.IOException {
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 = System.in.read();
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");
}
}
+Pie Number of slices to send: Send
The problem is that you misspelled "receive" -- "i before e except after c ... usually."

Okay, the real problem is that InputStream.read() 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 SavitchIn.java. 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.
WARNING! Do not activate jet boots indoors or you will see a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1093 times.
Similar Threads
Vending Machine Project
Change Due/Tendered Program
Vending Machine Project
Vending Machine.
Need help creating classes
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 09:03:57.