LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 04-24-2004, 02:24 AM   #1
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Rep: Reputation: 45
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?
 
Old 04-24-2004, 04:17 AM   #2
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
try casting thus:

Code:
public Float getValue(Integer discount){
                    return new Float( discount.floatValue() * price.floatValue());
                         }
 
Old 04-24-2004, 07:48 AM   #3
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Original Poster
Rep: Reputation: 45
Exclamation oooo

I works...now thx... But why where was my mistake?
Thx
Have a nice day
 
Old 04-24-2004, 09:26 AM   #4
Komakino
Senior Member
 
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938

Rep: Reputation: 55
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.
 
Old 04-24-2004, 02:10 PM   #5
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
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.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to convert a float to its binary or hex representation in Python? zero79 Programming 1 09-01-2005 10:19 AM
How to convert a float to its binary or hex representation in Python? zero79 Linux - Software 1 08-29-2005 09:30 PM
count digits of a float || convert float to string nadroj Programming 6 07-11-2005 04:52 PM
Convert Java to C++? leanieleanz Programming 6 03-04-2005 12:14 PM
Java | C | C++ - Float/Double question Mega Man X Programming 6 01-01-2004 10:14 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration