Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
04-24-2004, 02:24 AM
|
#1
|
Senior Member
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203
Rep:
|
can't convert o 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?
|
|
|
04-24-2004, 04:17 AM
|
#2
|
Senior Member
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120
Rep:
|
try casting thus:
Code:
public Float getValue(Integer discount){
return new Float( discount.floatValue() * price.floatValue());
}
|
|
|
04-24-2004, 07:48 AM
|
#3
|
Senior Member
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203
Original Poster
Rep:
|
oooo
I works...now thx... But why where was my mistake?
Thx
Have a nice day
|
|
|
04-24-2004, 09:26 AM
|
#4
|
Senior Member
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938
Rep:
|
Your mistake was that you defined the return type of getValue as Float (the class java.lang.Float - note the capital F) but you were trying to cast the integer into the java primitive type of float (lower case f). The two aren't the same thing. The Float is an actual class which contains all sorts of methods for dealing with floating point numbers; float is just a basic primitive type to hold a number, it doesn't have any methods: it isn't a class.
|
|
|
04-24-2004, 02:10 PM
|
#5
|
Senior Member
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120
Rep:
|
Yup, as it stands with java you can only cast primitive types, there's an execption, but overall this is the case, Float is a wrapper as mentioned, a class within itself, so java see's it and says, what ya on about, nobody told me you knew each other?
Big hint aswell, Sun havae provied online and downloadable probably the most clearest, accesbile and succint api documentation I have ever see 
Last edited by Looking_Lost; 04-24-2004 at 02:20 PM.
|
|
|
All times are GMT -5. The time now is 12:41 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|