Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-05-2007, 06:30 PM
Member
 
Join Date: Jul 2007
Posts: 40
cachi is on a distinguished road
Error with convert to Float
Hi, Please have a look in this small program and the error that compiler returns

Code:
package Package; /** * * @author cachi */ public class book { String title; String code; String last; String isbn; Integer quantity; Float price; /** Creates a new instance of book */ public book(String mytitle,String mycode,String mylast) { title=mytitle;mycode=code;mylast=last; } public book(String mytitle,String mycode,String mylast,String myisbn,Integer myquantity,Float myprice) { title=mytitle;code=mycode;last=mylast;isbn=myisbn;quantity=myquantity;price=myprice; } public void setTitle(String mytitle) {title=mytitle;} public void setCode(String mycode) {code=mycode;} public void setLast(String mylast) {last=mylast;} public void setIsbn(String myisbn) {isbn=myisbn;} public void setQuantity(Integer myquantity) {quantity=myquantity;} public void setPrice(Float myprice) {price=myprice;} public String getTitle(){return title;} public String getCode(){return code;} public String getLast(){return last;} public String getIsbn(){return isbn;} public Integer getQuantity(){return quantity;} public Float getPrice(){return price;} public Float getValue(Integer discount){return (float)(discount*price);} //Find out what to return here public void showBook (){} //How i must show the book? }
Code:
public Float getValue(Integer discount){return (float)(discount*price);} //Find out what to return here ^ Package/book.java [40:1] incompatible types found : float required: java.lang.Float public Float getValue(Integer discount){return (float)(discount*price);} //Find out what to return here ^ 2 errors
Thanks.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-06-2007, 04:18 AM
Senior Member
 
Join Date: Jul 2007
Posts: 130
cruxblack will become famous soon enough
I haven't tried ur code since i don't know what ur main program call upon the code, but from the error messages, it seems that the auto-boxing from float into Float wrapper class didn't work well
Maybe u could do this way
Code:
public Float getValue(Integer discount){return (Float)(discount*price);} //Find out what to return here
Btw, why must u use the Float wrapper instead of the primitive float?
It would be less memory consuming if u just use the primitives, u didn't use the toString() or any other wrapper method in ur code after all

I suggest using primitives for this one
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot convert from char to String error sondratheloser New To Java 1 12-13-2007 11:28 PM
type mismatch: cannot convert from double to float bugger New To Java 2 11-16-2007 03:24 PM
convert string to float miss_dot NetBeans 1 11-15-2007 01:26 AM
Class float Peter New To Java 1 07-08-2007 03:17 AM
Error: convert from String to long bbq New To Java 1 06-29-2007 09:23 PM


All times are GMT +3. The time now is 11:13 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org