Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-24-2004, 08:26 AM   PM User | #1
alaios
Regular Coder

 
Join Date: Aug 2002
Posts: 433
Thanks: 3
Thanked 0 Times in 0 Posts
alaios is an unknown quantity at this point
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?
alaios is offline   Reply With Quote
Old 04-25-2004, 08:31 AM   PM User | #2
black3842
New Coder

 
Join Date: Apr 2004
Location: Texas
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
black3842 is an unknown quantity at this point
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
black3842 is offline   Reply With Quote
Old 04-25-2004, 08:51 AM   PM User | #3
shmoove
Regular Coder

 
Join Date: Dec 2003
Posts: 367
Thanks: 0
Thanked 0 Times in 0 Posts
shmoove is an unknown quantity at this point
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
shmoove is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:19 AM.


Advertisement
Log in to turn off these ads.