5 Replies - 280 Views - Last Post: 31 July 2013 - 07:47 AM Rate Topic: -----

#1 6thSense  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 26-June 13

How to convert a double or a string to new data type

Posted 29 July 2013 - 03:38 PM

Hi guys,

Basically, I created a class which looks like this:

public class ValueObject {

	public static double val;
	public BufferedImage picture;
	
	//constructors
	public ValueObject(double val){
		this.val=val;
	}
	public ValueObject(BufferedImage picture){
		this.picture=picture;
	}
	
}


I'm using that class as a data type.
I'm trying to find to way to convert a string or a double (either solution is fine) to this data type.

Help would be greatly appreciated.

Thank you,

-6th

Is This A Good Question/Topic? 0
  • +

Replies To: How to convert a double or a string to new data type

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7641
  • View blog
  • Posts: 12,881
  • Joined: 19-March 11

Re: How to convert a double or a string to new data type

Posted 29 July 2013 - 03:43 PM

Your constructor will create a ValueObject with whatever double value you give it, so that's pretty well sorted.
(it won't "convert" the double in that 4.5 will still be 4.5 after you create that object, and if you have
double d = 4.5;
ValueObject v = new ValueObject (d);


then d will still be a double with value 4.5, but I think it'll do what you need it to do.

To create a ValueObject from a String, just use the Double.parseDouble() - it's in the Double wrapper class, you can look it up.
Was This Post Helpful? 0
  • +
  • -

#3 StrongJoshua  Icon User is offline

  • D.I.C Head

Reputation: 47
  • View blog
  • Posts: 156
  • Joined: 19-July 13

Re: How to convert a double or a string to new data type

Posted 29 July 2013 - 04:28 PM

Double.parseDouble(someString) will only work if someString is a string containing only numbers ("12034") otherwise you'll have get an error. To prevent that surround the parse with a try/catch clause.
Was This Post Helpful? 1
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7641
  • View blog
  • Posts: 12,881
  • Joined: 19-March 11

Re: How to convert a double or a string to new data type

Posted 29 July 2013 - 04:36 PM

View PostStrongJoshua, on 29 July 2013 - 06:28 PM, said:

Double.parseDouble(someString) will only work if someString is a string containing only numbers ("12034") otherwise you'll have get an error. To prevent that surround the parse with a try/catch clause.



This is true - as I say, look up the API documentation at Oracle's java site and come back with questions if you have any. You might have to learn some things about exception handling and number formats here. That's a good thing.
Was This Post Helpful? 0
  • +
  • -

#5 6thSense  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 26-June 13

Re: How to convert a double or a string to new data type

Posted 29 July 2013 - 07:16 PM

Thanks a lot guys. You guys are great. :)
Was This Post Helpful? 0
  • +
  • -

#6 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: How to convert a double or a string to new data type

Posted 31 July 2013 - 07:47 AM

View PostStrongJoshua, on 30 July 2013 - 12:28 AM, said:

Double.parseDouble(someString) will only work if someString is a string containing only numbers ("12034")


It'll also work for numbers in scientific notation like 3E+4 etc :P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1