• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

input is showing different than output

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic