I am currently learning JAVA using Aleks Rudenko book, and as a newbie in JAVA I still cannot figure out simple compilation errors. I tried to create a simple code that takes an array X and multiply every single term of it by Y. Finally, the array*Y should be printed out as follows:
public class CurrencyConverter {
double[] answers = new double[] {};
public double[] Times2 (double[] x, double y) {
for (int i=1; i<x.length+1;i++) {
answers.add(x[i]*y);
}
return answers;
}
public static void main(String[] args) {
CurrencyConverter cc = new CurrencyConverter();
double [] rates= new double[] {63,3.0,3.0,595.5,18.0,107.0,2.0,0.0};
System.out.println(cc.Times2(rates,2));
}
}
the error message is
error: cannot find symbol
answers.add(x[i]*y);
^
symbol: method add(double)
location: variable answers of type double[]
1 error
I cannot figure out what is the reason for this compilation error. Thanks for the help !
.dotoperator on an array.answersis an array and its values can be accessed likeanswers[0] -> first elementoranswers[n] -> gives n element. Hadanswersbeen an object with say a property calledstatus, then you would accessstatusfromanswersasanswers.status.answershave method saybestAnswer()then you would access it asanswers.bestAnswer()