简体   繁体   中英

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?

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);
        }
    }
}

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

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.

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.

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));
        }
    }

}

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);
    }
}

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]);
}
//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(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM