1

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 !

3
  • You are using . dot operator on an array. answers is an array and its values can be accessed like answers[0] -> first element or answers[n] -> gives n element. Had answers been an object with say a property called status, then you would access status from answers as answers.status . Commented May 5, 2020 at 0:40
  • Similar if answers have method say bestAnswer() then you would access it as answers.bestAnswer() Commented May 5, 2020 at 0:41
  • (added a bit more detailed answer below as comments have characters limit) Commented May 5, 2020 at 0:46

4 Answers 4

1

add is not defined for arrays. If you want to add elements, use List.

There are several ways to fix your code:

a) use List:

import java.util.*;

List<Double> list = new ArrayList<Double>();

public List<Double> Times2 (double[] x, double y) {
    for (int i=0; i<x.length;i++) {
      list.add(x[i]*y);
    }
    return list;
}

b) Don't add elements, just write them into array:

double[] answers;

public double[] Times2 (double[] x, double y) {
    answers = new double[x.length];
    for (int i=0; i<x.length;i++) {
      answers[i] = x[i]*y;
    }
    return answers;
}

..Also, watch out for the array index out of bounds. First element has index 0 and last item has index length - 1.

And one last thing, you will probably encounter problem with printing your results. If you want to use list, use System.out.println(cc.Times2(...).toString()); and if you want to use array, use System.out.println(Arrays.toString(cc.Times2(...)));

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

2 Comments

Probably ArrayList or Vector, not List
I generally recommend using List as it's faster.
1

Better to use a while loop, instead of a for loop, for this use case. And use a Vector:

   Vector<Object> answers = new Vector<Object>();

2 Comments

Don't use Vector, unless you actually need the synchronization. Use ArrayList.
Doesn't really matter, for this one, either Vector or ArrayList
1

You are using . dot operator on an array. answers is an array and its values can be accessed like answers[0] -> first element or answers[9] -> gives 9th element. Had answers been an object with say a variable called status, then you would access status from answers as answers.status

Similar if answers have method say bestAnswer() then you would access it as answers.bestAnswer()

And if you want to hold a bunch of objects of your custom type Answer in an array then you would instantiate each Answer object first and add it to the array answers array as:

Answer firstAnswer = Answer(); 
Answer secondAnswer = Answer();
...

answers[0] = firstAnswer;
answers[1] = secondAnswer;
...

Comments

1

Arrays in java don't have any methods. You access stuff by index. Your code should look

public static class CurrencyConverter {

    public double[] Times2(double[] x, double y) {
        double[] answers = new double[x.length];
        for (int i=0; i < x.length; i++) {
            answers[i] = x[i] * y;
        }
        return answers;
    }

}

Notice that the for loop starts with i as 0. the first index of an array is always 0, not 1. the answers array is init'ed inside the method as it's best to keep variables hidden (scoped) from things that don't need them. it's also init'ed with the size it needs to be.

Some of the other answers here say to use stuff based on java.util.AbstractCollection. While you could but there's no good reason for extra overhead for something simple like this.

Comments

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.