Get your CodeRanch badge!*
The moose likes I/O and Streams and the fly likes input is showing different than output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "input is showing different than output" Watch "input is showing different than output" New topic
Author

input is showing different than output

John Swain
Greenhorn

Joined: Feb 12, 2005
Posts: 16
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");
}
}
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
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.
 
wood burning stoves
 
subject: input is showing different than output
 
Similar Threads
Change Due/Tendered Program
Need help creating classes
Vending Machine Project
Vending Machine Project
Vending Machine.