cc0
cc0

Reputation: 1950

Java - converting String in array to double

I'm stuck with this pretty silly thing;

I got a textfile like this;

Hello::140.0::Bye

I split it into a string array using;

LS = line.split("::");

Then I try to convert the array values containing the number to a double, like this;

Double number = Double.parseDouble(LS[1]);

But I get the following error message;

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1

Does anyone have any idea why this doesn't work?

Upvotes: 2

Views: 13866

Answers (8)

Yeabkal Wubshit
Yeabkal Wubshit

Reputation: 41

//Using streams provides a neat solution.
// this will give you {1.3, 3.5, 98.342}
String input = "1.3::3.5::98.342";
double[] arr = Arrays.stream(input.split("::")).mapToDouble(Double::parseDouble).toArray(); 

Upvotes: 0

JAN
JAN

Reputation: 21855

import java.util.ArrayList;
import java.util.Arrays;

public class StringToDoubles {

    /**
     * @param args
     */
    public static void main(String[] args)
    {

        String CommaSeparated = "22.3 , 24.5, 44.3";

        java.util.List<String> items = Arrays.asList(CommaSeparated.split("\\s*,\\s*"));
        java.util.List<Double> out = new ArrayList<Double>(); 

        for(int i=0; i < items.size();i++)
        {
            double d = Double.parseDouble(items.get(i));
            System.out.println(d);
            out.add(new Double(d));
        }
    }

}

Upvotes: 0

anamitra deb
anamitra deb

Reputation: 41

public class Test
{

    public static void main(String[] args)
    {
        String s = "140.0::156.33::155.89";
        String[] ls;
        ls = s.split("::");
        for(int i=0;i<=2;i++)
        {
            //System.out.println(ls[i]);
        double d = Double.parseDouble(ls[i]);
           System.out.println(d);
        }
    }
}

Upvotes: 4

Alphie
Alphie

Reputation: 36

You should a compiler, such as Eclipse, which allows you to debug the code dynamically. The issue here is that the Array LS has a size <=1. This could be caused by incorrectly reading the text file as Halo mentioned or as Mnementh mentioned by having a blank line. This will infact occur for any line that does not contain the :: delimiter.

You should perform a check to ensure that the Array LS contains >X entries, especially if you are reading in from a file where entries could vary.

double number = 0.0
if( LS.size() > 3 ){
    number = Double.parseDouble(LS[1]);
}

Upvotes: 0

Mnementh
Mnementh

Reputation: 51311

I think some lines don't contain data in the specified format. Does the file contains empty lines? In this case the split returns only a single-element array. To find the problem, you could print the line (of the data), on which the error occurs.

Upvotes: 1

Daniel Wedlund
Daniel Wedlund

Reputation: 814

what type is LS?? This code works for me:

public class Test
{

    public static void main(String[] args)
    {
        String s = "Hello::140.0::Bye";
        String[] ls;
        ls = s.split("::");
        System.out.println(ls[1]);
        double d = Double.parseDouble(ls[1]);
        System.out.println(d);
    }
}

Upvotes: 0

Halo
Halo

Reputation: 1532

I think it's a problem with reading the file. Did you try printing line and also the contents of LS after splitting?

Upvotes: 4

Paul Tomblin
Paul Tomblin

Reputation: 182772

That error has nothing to do with that line of code. Look for somewhere where you're trying to access index 3 of an array. LS in this example should only have indexes 0-2.

Upvotes: 2

Related Questions