View Full Version : convert to Float in Java

04-24-2004, 08:26 AM
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) {
public book(String mytitle,String mycode,String mylast,String myisbn,Integer myquantity,Float 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?

04-25-2004, 08:31 AM
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.

04-25-2004, 08:51 AM
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.