Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Aug 2002
    Posts
    433
    Thanks
    3
    Thanked 0 Times in 0 Posts

    convert to Float in Java

    Plz have a look in this small program and the error that compiler returns

    /*
    * book.java
    *
    * Created on 09 April 2004, 01:07
    */

    package Package;

    /**
    *
    * @author alaios1
    */
    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?
    }


    /*---Compiler Erros --- */


    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
    Errors compiling book.


    As you can see i cannot conver to float.... Why ..? What i must do?

  • #2
    New Coder
    Join Date
    Apr 2004
    Location
    Texas
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I might very well be wrong, but I don't think you have to cast that. I think when you multiply a float times an integer you get a float result.

    just a thought.
    Jason B

  • #3
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Float (with a capital 'F') is a wrapper object for floats (a class). float is a primitive type. They are not the same, and you can't just interchange them. From a quick glance at your program, I see that you don't really need to use the wrapper object, so you should change all the "Float"'s in your program to "float"'s.

    shmoove


  •  

    Posting Permissions

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