Question

I have the following code where I want to catch the values of doub and put them into an array. As a beginner I'm trying to get all the outputs of the variable doub and put them into the array using the line...

double[] x = new double[(int) doub];

and although this code compiles I just get values like [D@116ab4e when I print x out. I guess this is because I am casting the double as an int but its double. However if I dont do this the code doesn't compile. Im just plain stuck! so any suggestions would be appreciated thanks!

                    String[] shadingarray = str.split(";");

                    for (String str1: shadingarray){
                        Matcher search = Pattern.compile("(?!=\\d\\.\\d\\.)([\\d.]+)").matcher(str1); 
                        while (search.find()){ 
                            double doub = Double.parseDouble(search.group(1)); 

                            double[] x = new double[(int) doub];

I should also add the type of doubles that doub equals, they are like the following:

3.0
6.4855612
4.0
0.6161728763
4.0
2.2012378517
4.0

Any help?

Was it helpful?

Solution

You'll probably want to rework your loop:

List<Double> list = new ArrayList<Double>()
while ( search.find() ) { 
    list.add( Double.parseDouble(search.group(1)) )
}

if you want it as an array, then

double x[] = list.toArray();

Also if you System.out.println( x ) you're going to get something like [D@116ab4e, which essentially means "array of double" ([D) followed by the hashcode of your array object. You'll probably want to

for ( i=0; i<x.length; i++ ) { System.out.println( x[ i ] ); }

Cheers,

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top