2

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?

2
  • Could you post full method code? I think your error occurs in the other line. Commented Mar 16, 2010 at 12:12
  • I don't see anything that would cause that exception. What are you doing with the rest of the elements in LS? Commented Mar 16, 2010 at 12:12

8 Answers 8

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, the line prints fine with the correct value, even after that parseDouble conversion.
Ahhh, I just had an empty line in the textfile, DOH! Thanks for the help!
4
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);
        }
    }
}

Comments

2

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.

1 Comment

Oops, my bad, I generalized my own code, the exception now refers to the correct index.
1

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.

Comments

0

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

Comments

0

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

Comments

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

}

Comments

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

2 Comments

Can you explain why this is better than the other 7 answers?
It is not strictly "better" than others. Functional programming can be used through this approach. .forEach(s->System.out.println(s)) can be used, for example, to print the doubles, all in one line of code and making use of the efficiency of streams.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.