Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Conversion Error
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
S_Grimm




PostPosted: 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.


Pennies.zip
 Description:
The Converter.java and read.java files

Download
 Filename:  Pennies.zip
 Filesize:  2.38 KB
 Downloaded:  94 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Thu Oct 16, 2008 5:30 pm   Post subject: RE:Conversion Error

code:

int numberOfPennies = pennies.length ();
...
 if ((numberOfPennies >= 1000) && (numberOfPennies <= 9999))

Are you expecting someone to type in 9999 characters worth of pennies?

Besides
dollars = pennies div 100;
cents = pennies mod 100;
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
S_Grimm




PostPosted: 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 Sad

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 Sad
Tony




PostPosted: 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
code:

int numberOfPennies = ...
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
DemonWasp




PostPosted: 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.
Insectoid




PostPosted: 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,

code:

this = that/foo

where 'that' and 'foo' are integers. Basically, Java divides them and cuts of the decimal.

Modulus in java is the '%'. so,

code:

that = this % foo
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: