Results 1 to 2 of 2
  1. #1
    cachi is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default Error with convert to Float

    Hi, Please have a look in this small program and the error that compiler returns

    Java 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?
    }
    Java 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.

  2. #2
    cruxblack is offline Senior Member
    Join Date
    Jul 2007
    Posts
    130
    Rep Power
    0

    Default

    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
    Java 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

Similar Threads

  1. Cannot convert from char to String error
    By sondratheloser in forum New To Java
    Replies: 1
    Last Post: 12-13-2007, 10:28 PM
  2. Replies: 2
    Last Post: 11-16-2007, 02:24 PM
  3. convert string to float
    By miss_dot in forum NetBeans
    Replies: 1
    Last Post: 11-15-2007, 12:26 AM
  4. Class float
    By Peter in forum New To Java
    Replies: 1
    Last Post: 07-08-2007, 02:17 AM
  5. Error: convert from String to long
    By bbq in forum New To Java
    Replies: 1
    Last Post: 06-29-2007, 08:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •