Java Code

Associate
Joined
15 Nov 2004
Posts
92
Location
The land of Eng
I can't seem to get this code to work:

Code:
public void areaRectangle()
  {

      double area;
      double itsHeight;
      double itsWidth;
      
      System.out.println("Okay, so you want to calculate a Rectangle");   

      itsHeight = readString("Enter the height of the rectangle");
      itsWidth = readString("Enter the width of the rectangle");
                           
      area = itsWidth * itsHeight;
      
      System.out.println("The area of the rectangle is:" + area);    


  }

It keeps saying:

incompatible types; found : java.lang.string, required; double at line 55, column 19 (The line were " itsHeight = readString("Enter the height of the rectangle");" is)
 
Thats because readString returns a string and you are assigning it to a double. . .like doing:

double name = "hello"

which obviously isn't allowed.

You need to convert the string to a double representation. There are a few conversion methods available in the Double class. Try:

try{
itsHeight = Double.valueOf(readString("Enter the height of the rectangle"));
itsWidth = Double.valueOf(readString("Enter the width of the rectangle"));
}
catch(NumberFormatException e)
{
System.out.println("String was not in correct format");
}

area = itsWidth * itsHeight;


...
 
as said java is after an integer of type double at line 55

instead its finding a variable of type string... just change them to read in integer values and it should get rid of that error :)
 
Code:
public void areaRectangle()
  {

      double area;
      double itsHeight;
      double itsWidth;
      
      System.out.println("Okay, so you want to calculate a Rectangle");   

      itsHeight = Double.parseDouble(readString("Enter the height of the rectangle"));
      itsWidth = Double.parseDouble(readString("Enter the width of the rectangle"));
                           
      area = itsWidth * itsHeight;
      
      System.out.println("The area of the rectangle is:" + area);    


  }
 
Dj_Jestar said:
Code:
public void areaRectangle()
  {

      double area;
      double itsHeight;
      double itsWidth;
      
      System.out.println("Okay, so you want to calculate a Rectangle");   

      itsHeight = Double.parseDouble(readString("Enter the height of the rectangle"));
      itsWidth = Double.parseDouble(readString("Enter the width of the rectangle"));
                           
      area = itsWidth * itsHeight;
      
      System.out.println("The area of the rectangle is:" + area);    


  }


Ahh yes you want parseDouble not valueOf (valueOf returns a Double, parseDouble returns a double, but is essentially the same thing). Of course as usual there is no real reason for this thread. . .the Java API clearly has everything you need to know here:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Double.html

ps: Being picky, you should catch exceptions for NumberFormatException around the parsing lines and handle them sensibly.
 
I figured it out in the end. Thanks to everyone that responded. You rock. :cool:

It's a little silly what I've had to do -- My tutor for the programming side of my degree decided that he'd make us create a program in Java using some VB code as an example and put this as half of the marks for the final assignment. Yet we've done 99.5% VB coding this year and only an hour on Java coding. What an idiot.

I've had to basically teach myself how to code as my teacher is about as helpful as smacking your face with a plank of wood repeatedly. :(
 
Back
Top Bottom