Reputation: 547
hope everyone is well.
I am getting this error when calling a method that I've created. I reason why I created the method is to return 2 double values, so I used a 2d array. Here is my relevant code:
Double[][] type2FS;
type2FS = new Double[rule.getRules().get(x).getAttributes().size()][rule.getRules().get(x).getAttributes().size()];
int pos=0;
the 2d array is the size of the number of attributes in a Decision Tree Rule, the rules are stored in an ArrayList.
type2FS[pos][pos]=calRight(grade.getDM(),grade.getDNU(),grade.getDNL(),Double.parseDouble(store.getData()[i][z]));
pos++;
break;
I get the error in the part after the '=' sign, with the message "Type mismatch: cannot convert from Double[][] to Double". There are 4 double variables that are passed.
private Double[][] calRight(double dm, double dnu, double dnl, double value)
{
double upperMg= (value-dnl) / (dnu-dnl);
double lowerMg= 1 - upperMg;
if(upperMg>1.0)
{
upperMg=1.0;
}
else if (upperMg<0.0)
{
upperMg=0.0;
}
if(lowerMg>1.0)
{
lowerMg=1.0;
}
else if(lowerMg<0.0)
{
lowerMg=0.0;
}
Double[][] temp = {{upperMg},{lowerMg}};
return temp;
}
This is the method that calculates the 2 double values that I wish to store in my type2FS 2D array.
The 2 double values that are returned from the function are part of a set. So one set looks like (4.5,5.5), now one decision tree rule can have many sets depending on the rule. So I thought the best way to store this was in a 2D Array.
I cant understand where I'am going wrong. Any help would be greatly appreciated.
Thank you
Upvotes: 0
Views: 270
Reputation: 11131
type2FS[pos][pos] = ...
is trying to assing a value to a single "cell" of your array, so it should be a double value. But you're passing an array instead.
So, change
type2FS[pos][pos]=calRight(grade.getDM(),grade.getDNU(),grade.getDNL(),Double.parseDouble(store.getData()[i][z]));
to
type2FS=calRight(grade.getDM(),grade.getDNU(),grade.getDNL(),Double.parseDouble(store.getData()[i][z]));
Anyway, "[...] to return 2 double values, so I used a 2d array" doesn't make sense, you could use a 1d array as well:
private Double[] calRight(double dm, double dnu, double dnl, double value) {
//other code
Double[] temp = {upperMg,lowerMg};
return temp;
}
even though that's not the best solution anyway.
Upvotes: 1
Reputation: 41210
Error - 'Cannot Convert Double[][] To Double"
yes it, why! type2FS[pos][pos]
is double type reference & calRight(...)
returns double[][]. It is type mismatch.
type2FS[pos][pos]=calRight(grade.getDM(),grade.getDNU(),grade.getDNL(),Double.parseDouble(store.getData()[i][z]));
Upvotes: 1
Reputation: 2006
type2FS[pos][pos]=calRight(grade.getDM(),grade.getDNU(),grade.getDNL(),Double.parseDouble(store.getData()[i][z]));
It means i value is pos and j value is pos , then you are going to assign particular row and and column value then its boolean.
instead of that
type2FS =calRight(grade.getDM(),grade.getDNU(),grade.getDNL(),Double.parseDouble(store.getData()[i][z]))
you can call like that.
type2FS[pos] is expecting one dimension array. Because you are mentioning only row.
type2FS[pos][[pos] is expecting Double value. Because you are mentioning row ans column.
Upvotes: 1