corsetti
corsetti

Reputation: 77

cannot append to my array during a for loop

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 !

Upvotes: 1

Views: 388

Answers (4)

denov
denov

Reputation: 12698

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.

Upvotes: 1

nabster
nabster

Reputation: 1675

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;
...

Upvotes: 1

Ecto
Ecto

Reputation: 1147

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(...)));

Upvotes: 1

user13087176
user13087176

Reputation:

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>();

Upvotes: 1

Related Questions