-1

I am getting the ArrayIndexOutOfBoundException here. What am I missing ?

My code is below:

public class Exercise {

    public static double[] strfloat(String str){
        double[] f = new double[] {};
        String[] st = new String[] {};
        st= str.split(" "); 
        for(int i = 0; i < st.length; i++){ 
            System.out.println(st[i]); 
            f[i] = Double.parseDouble(st[i]);
            System.out.println(f[i]);
        }
        return f;
    }

    public static void main(String[] args) {
        String str = "1.99999996e-002 7.49999983e-003 0. 1.75000001e-002 9.99999978e-003";
        double[] retstr = strfloat(str);

        for(int i = 0; i < retstr.length; i++){
            System.out.println(retstr[i] + " ");
        }
    }
johnchen902
  • 9,531
  • 1
  • 27
  • 69
Shah
  • 661
  • 2
  • 9
  • 19

7 Answers7

2

Try doing this:

String[] st = str.split(" ");
double[] f = new double[st.length];

In that way, you initialize the array f to the size of the string chunks.

darijan
  • 9,725
  • 25
  • 38
1

The

double[] f = new double[] {};

is empty and you're referencing it's (non-existing) elements.

System.out.println(f[i]);

Before you print f[i], make sure it even exists.

You can fix it like that:

String[] st = str.split(" ");
double[] f = new double[st.length];
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
1

do this

String[] st = new String[]{};

st= str.split(" ");

double[] f = new double[st.length()];

Reason Behind this

ArrayIndexOutOfBoundsException is Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array

Here you have initialized the double array f with empty constant initializer. so the length of f is 0. But you are trying to acess f[i]. That's why the problem occured

stinepike
  • 54,068
  • 14
  • 92
  • 112
0

Initialize the f array:

    double[] f = new double[st.length];

Now you may wonder why you don't have to do this to st if you declared it in a similar way? In fact, it's not necessary to instantiate st as you did, because str.split() returns a different array instance anyway!

    String[] st = str.split(" ");
Balázs Németh
  • 6,222
  • 9
  • 45
  • 60
0

You are creating an empty array here:

double[] f = new double[] {};

Create your array using st.length, after the split. That way you'll have the size you want.

Diogo Moreira
  • 1,082
  • 2
  • 9
  • 24
0

arrayoutofboundexception here. What i am missing ?

You miss the end of f...

double[] f = new double[] {};

[...]
for(int i=0; i<st.length; i++){
    [...]
    f[i] = Double.parseDouble(st[i]);

f has a length of 0 so you exceed its length when you try to access f[0]

rearrange your code a bit:

String[] st = st= str.split(" ");
double[] f = new double[st.length];
Marco Forberg
  • 2,634
  • 5
  • 22
  • 33
0

You need to initialize double array f. Try this.

double[] f = new double[st.length].

In your code:

public static double[] strfloat(String str) {
        String[] st = new String[]{};
        st = str.split(" ");
        double[] f = new double[st.length];
        for (int i = 0; i < st.length; i++) {
            System.out.println(st[i]);
            f[i] = Double.parseDouble(st[i]);
            System.out.println(f[i]);
        }
        return f;
    }

    public static void main(String[] args) {

        String str = "1.99999996e-002 7.49999983e-003 0. 1.75000001e-002 9.99999978e-003";
        double[] retstr = strfloat(str);
        for (int i = 0; i < retstr.length; i++) {
            System.out.println(retstr[i] + " ");
        }
    }
Prabhakar Manthena
  • 2,223
  • 3
  • 16
  • 30
  • if my string contains "[" how can i ignore this. eg. str = "[ 1.99999996e-002 ... ]". I tried it with this code but getting some errors. Code: for(int i=0; i – Shah May 24 '13 at 14:24