• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

input is showing different than output  RSS feed

 
Greenhorn
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 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");
}
}
 
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 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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!