-
incompatible types ?
I get this error:
incompatible types
found : double
required: java.lang.String
zwembad = Lengte + Breedte + Diepte;
Can anyone tell me what this means?
i have checked my code several times.
This is my code if you are interrested:
// Its a swimming pool that uses length, width and depth.
content is calculated with: length, width and depth
import java.lang.String;
public class Zwembad
{
double Lengte;
double Breedte;
double Diepte;
double Inhoud;
//Methode
public Zwembad(double zbLengte, double zbBreedte, double zbDiepte)
{
Lengte = zbLengte;
Breedte = zbBreedte;
Diepte = zbDiepte;
}
//Methode
public double setLengte ()
{
return Lengte;
}
public double setBreedte ()
{
return Breedte;
}
public double setDiepte ()
{
return Diepte;
}
public double getinhoud ()
{
return (Lengte + Breedte + Diepte);
}
public String toString()
{
String zwembad;
zwembad = Lengte + Breedte + Diepte;
return zwembad;
}
}
this is the main file thats uses "zwembad"
public class Main {
public static void main (String [ ] argv)
{
Zwembad z = new Zwembad (2.0, 5.5, 1.5);
System.out.println (z.getInhoud());
}
}
-
String zwembad;
zwembad = Lengte + Breedte + Diepte;
return zwembad;
You're adding 3 doubles together and assigning it to a String variable. If you add anything to a string however, it automatically gets casted into a string and concatenated.
this should work.
zwembad = "" + Lengte + Breedte + Diepte;
-
your code
well check this out
here we go :
import java.lang.String;
public class Zwembad
{
double Lengte;
double Breedte;
double Diepte;
double Inhoud;
//Methode
public Zwembad(double zbLengte, double zbBreedte, double zbDiepte)
{
Lengte = zbLengte;
Breedte = zbBreedte;
Diepte = zbDiepte;
}
//Methode
public double setLengte ()
{
return Lengte;
}
public double setBreedte ()
{
return Breedte;
}
public double setDiepte ()
{
return Diepte;
}
public double getinhoud ()
{
double p;
p=Lengte + Breedte + Diepte;
return p;
}
public String toString()
{
String zwembad;
zwembad =" "+ Lengte + " "+Breedte + ""+Diepte+" ";
return zwembad;
}
}
//this is the main file thats uses "zwembad"
class Main {
public static void main (String [ ] argv)
{
Zwembad z = new Zwembad (2.0, 5.5, 1.5);
System.out.println (z.getinhoud());
}
}
-
Code:
public String toString()
{
String zwembad;
zwembad =" "+ Lengte + " "+Breedte + ""+Diepte+" ";
return zwembad;
}
you only have to add the "" once, just like in Phaelax example.
-
well thats because this are the only thing to change in you're code
in other word System.out.println ("Should be applayed to String type only")
you got it?
that 's why we're taking the String value of your variables
it compil fine
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks