Bakslat Bakslat - 1 year ago 69
Java Question

Converting string to double error

I have a problem converting a basic float array to string and then to double.

I have:

private static float[] getSample() {

float[] sample = new float[sampleSize];

// Gets the sample an returns it
sampleProvider.fetchSample(sample, 0);
return sample;

Which return a float[] then I use:

String sampleColor = Arrays.toString(getSample());

This for example returns: [0.62]. I think the problem is with the square brackets
I can verify by using:


But when I try to convert it to double it just gives me an error. I try to convert it using parseDouble:

double colorT = Double.parseDouble(sampleColor);

Answer Source

if the [] are in the string while parsing it will throw an exception. to parse it you will have to remove them first. example:samplecolor=sampleColor.replaceAll("[\\[\\]]", "");

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download