Computer Science Canada Conversion Error |
Author: | S_Grimm [ Thu Oct 16, 2008 5:14 pm ] |
Post subject: | Conversion Error |
This program is supposed to convert a number of pennies into dollars and cents. It's not. ill post the code, and if anyone can find the flaw... I'd appreciate learning what i did wrong. Also, you need to run the read.java file BEFORE trying converter.java. the reason is that my statements for string input are in the read.java file. |
Author: | Tony [ Thu Oct 16, 2008 5:30 pm ] | ||
Post subject: | RE:Conversion Error | ||
Are you expecting someone to type in 9999 characters worth of pennies? Besides dollars = pennies div 100; cents = pennies mod 100; |
Author: | S_Grimm [ Thu Oct 16, 2008 6:13 pm ] |
Post subject: | RE:Conversion Error |
no not really. but it is good to be prepared for any eventuality. and pennies is a string, not a int. so div and mod won't work ![]() edit: thanks tony. i realise that my code says if the Length of the STRING is greater than 10, etc what i needed it to say was if the number of pennies is greater than 9999.... edit 2: what do you mean by mod? the dollars seems to work fine by i can't get pennies to work ![]() |
Author: | Tony [ Thu Oct 16, 2008 7:42 pm ] | ||
Post subject: | Re: RE:Conversion Error | ||
mod as in modulus, also known as "remainder". A\V @ Thu Oct 16, 2008 6:13 pm wrote: pennies is a string, not a int
Well there's your problem. Though your code does say the variable to be int
|
Author: | DemonWasp [ Fri Oct 17, 2008 9:00 am ] |
Post subject: | RE:Conversion Error |
A\V, you will want to convert the String value to an integer value. Try using Integer.parseInt(). Then you'll have an int value to work with, so division and modulus will work, as per Tony's post. |
Author: | Insectoid [ Fri Oct 17, 2008 11:12 am ] | ||||
Post subject: | RE:Conversion Error | ||||
Div will return the number of times a number can be divided into a number. Div in java is just '/'. so,
where 'that' and 'foo' are integers. Basically, Java divides them and cuts of the decimal. Modulus in java is the '%'. so,
|