7 Replies - 1011 Views - Last Post: 14 March 2010 - 07:21 PM Rate Topic: -----

#1 livinlife  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 14-March 10

incompatible types

Posted 14 March 2010 - 03:06 PM

I have about 5 or 6 errors when I compile and they all say

incompatible types
found : java.lang.String
required: double

Anyone know how to fix this? I'll post snippets of code if I need to or the whole thing if necessary.

Thanks in advance.
Is This A Good Question/Topic? 0
  • +

Replies To: incompatible types

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10780
  • View blog
  • Posts: 40,141
  • Joined: 27-December 08

Re: incompatible types

Posted 14 March 2010 - 03:08 PM

Your error is telling you that you cannot assign a String to a double variable. You can try to convert your String to a double by using the Double.parseDouble(String) method, which will work so long as the only characters in the String are numeric, a single decimal point (at most), and up to a single hyphen at the beginning of the String. Otherwise, the parseDouble() method will throw a NumberFormatException.
Was This Post Helpful? 0
  • +
  • -

#3 skibo25  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 35
  • Joined: 12-May 09

Re: incompatible types

Posted 14 March 2010 - 03:09 PM

Basically you are trying to say that a string is a double. What you need to do is convert the string to a double like so;

double double = Double(YourString)


Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10780
  • View blog
  • Posts: 40,141
  • Joined: 27-December 08

Re: incompatible types

Posted 14 March 2010 - 03:13 PM

@Skibo25: Don't forget that you can't name your variables using reserved words in Java. In addition, you need to use the new keyword when creating a new Double object. And since you're creating a Double object, you can't use a primitive variable to reference it, as primitives only hold values. Lastly, you have to include a semi-colon at the end of all statements in Java. So your code should read:
Double myDouble = new Double(someString);
double toPrim = myDouble; //autoboxing converts myDouble to a primitive double


Was This Post Helpful? 0
  • +
  • -

#5 skibo25  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 35
  • Joined: 12-May 09

Re: incompatible types

Posted 14 March 2010 - 03:21 PM

Whoa whoops yea lots of errors there. I honestly did not know about creating a Double object first, but the others were just not reading it twice.
Was This Post Helpful? 0
  • +
  • -

#6 livinlife  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 14-March 10

Re: incompatible types

Posted 14 March 2010 - 03:22 PM

@macosxnerd101

I'm new to Java so I'm still a little confused but I'm going to attempt to do what you advised. Thanks!
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10780
  • View blog
  • Posts: 40,141
  • Joined: 27-December 08

Re: incompatible types

Posted 14 March 2010 - 03:25 PM

View Postmacosxnerd101, on 14 March 2010 - 06:13 PM, said:

@Skibo25: Don't forget that you can't name your variables using reserved words in Java. In addition, you need to use the new keyword when creating a new Double object. And since you're creating a Double object, you can't use a primitive variable to reference it, as primitives only hold values. Lastly, you have to include a semi-colon at the end of all statements in Java. So your code should read:
//autoboxing converts the new Double object to a primitive double
double toPrim = new Double(someString); 



I need to correct some misinformation from my above post. Due to autoboxing, Java will convert the Double object here: new Double(someString); to a primitive double, so the statement double toPrim = new Double(someString); is legal. My mistake. I apologize for any confusion I may have caused.
Was This Post Helpful? 1
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: incompatible types

Posted 14 March 2010 - 07:21 PM

View Postlivinlife, on 14 March 2010 - 04:22 PM, said:

@macosxnerd101

I'm new to Java so I'm still a little confused but I'm going to attempt to do what you advised. Thanks!

just post your code
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1