d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > String To Double
12Next
Add Reply New Topic New Poll
Member
Posts: 2,264
Joined: Jul 7 2007
Gold: 26.00
Nov 3 2014 03:10pm
Quick Question;

If I have a string of numbers for example:

18.0 8 307.0 130.0 3504. 12.0 70 1

How would i convert each of these numbers to a double and put each one into a separate double array?

This post was edited by axumo0 on Nov 3 2014 03:10pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 3 2014 03:22pm
if it is just a single string, break each number into tokens/substrs based on ws delim

then do like

Code
myDoubleArray.pushBack(Double.parseDouble(substr));


(that is the JAVA function)

This post was edited by Eep on Nov 3 2014 03:23pm
Member
Posts: 2,264
Joined: Jul 7 2007
Gold: 26.00
Nov 3 2014 03:25pm
Quote (Eep @ Nov 3 2014 05:22pm)
if it is just a single string, break each number into tokens/substrs based on ws delim

then do like

Code
myDoubleArray.pushBack(Double.parseDouble(substr));


(that is the JAVA function)



Basically I'm doing this for a whole file. The numbers that correspond to each other go vertically that's why I need each one of these in a separate array. I can't get my program to read doubles from the text as I keep getting type mismatch exceptions but I can take it in as String and that seems to work but now I still need each number separately and in double form.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 3 2014 03:32pm
Quote (axumo0 @ Nov 3 2014 04:25pm)
Basically I'm doing this for a whole file. The numbers that correspond to each other go vertically that's why I need each one of these in a separate array. I can't get my program to read doubles from the text as I keep getting type mismatch exceptions but I can take it in as String and that seems to work but now I still need each number separately and in double form.


well, right.

That's why I said you need to split the BIG string by using something like a substring function to get each individual number you want as a string. It seems they are separated by whitespace, so that would be your delimiter.

This is one solution that I can think of off the top of my head.

This post was edited by Eep on Nov 3 2014 03:35pm
Member
Posts: 2,264
Joined: Jul 7 2007
Gold: 26.00
Nov 3 2014 03:38pm
Quote (Eep @ Nov 3 2014 05:32pm)
well, right.

That's why I said you need to split the BIG string by using something like a substring function to get each individual number you want as a string. It seems they are separated by whitespace, so that would be your delimiter.

This is one solution that I can think of off the top of my head.


I think I got it what i did was,

String[] tokens = input.split("\\s+");
And then just add each value I need into an array
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 3 2014 03:41pm
Quote (axumo0 @ Nov 3 2014 04:38pm)
I think I got it what i did was,

String[] tokens = input.split("\\s+");
And then just add each value I need into an array


yep! using that Double.parseDouble(), it will convert your numeric strings into the double format.
Member
Posts: 2,264
Joined: Jul 7 2007
Gold: 26.00
Nov 3 2014 04:16pm
Quote (Eep @ Nov 3 2014 05:41pm)
yep! using that Double.parseDouble(), it will convert your numeric strings into the double format.



Damn now i have another problem. it actually doesn't split properly it put its all into tokens[0] instead of each String at a separate index
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 3 2014 04:20pm
Quote (axumo0 @ Nov 3 2014 05:16pm)
Damn now i have another problem. it actually doesn't split properly it put its all into tokens[0] instead of each String at a separate index


hrm, that is weird.

Looking at SO, I see this example:

Code
String string = "004-034556";
String[] parts = string.split("-");
String part1 = parts[0]; // 004
String part2 = parts[1]; // 034556


which seems to do what it should and is similar to yours

edit: maybe check your regex? you are doing
Code
"\\s+"


shouldn't it be

Code
"\s+"


http://rubular.com/ pretty useful site for checking regex

This post was edited by Eep on Nov 3 2014 04:27pm
Member
Posts: 2,264
Joined: Jul 7 2007
Gold: 26.00
Nov 3 2014 04:28pm
Quote (Eep @ Nov 3 2014 06:20pm)
hrm, that is weird.

Looking at SO, I see this example:

Code
String string = "004-034556";
String[] parts = string.split("-");
String part1 = parts[0]; // 004
String part2 = parts[1]; // 034556


which seems to do what it should and is similar to yours

edit: maybe check your regex? you are doing
Code
"\\s+"


shouldn't it be

Code
"\s+"



Can't do "\s+" says invalid escape sequence

Basically this is my first string

218.0 178.0 9 0.11111111 0.0 0.8333327 0.54772234 1.1111094 0.5443307 59.62963 52.444443 75.22222 51.22222 -21.555555 46.77778 -25.222221 75.22222 0.31899637 -2.0405545 6

I did this just by reading the first line of the file

This post was edited by axumo0 on Nov 3 2014 04:32pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 3 2014 04:32pm
Quote (axumo0 @ Nov 3 2014 05:28pm)
Can't do "\s+" says invalid escape sequence


:wacko: huh...

you are correct, after looking online, "\\s+" is the correct java convention.



Maybe post your input file?

I am heading to class and maybe someone else can help you finish it. String.split() should net you the proper array though if the regex worked for your input file, so I am not sure what you got everything at index 0.
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll