0

It says that it cant find symbol referring to arraylist commands, like ".get" ".add".

import java.util.ArrayList;
import java.util.Iterator;

public class ProductPrices {
        
        private ArrayList<String> products = new ArrayList<String>();
        private ArrayList<Double> prices = new ArrayList<Double>();

        public ProductPrices(String products, double prices) {
            
            //this.products() = products();
            //this.prices() = prices();
        }

        public void put(String products, double prices) {
            
            for(int i=0; i<products.size(); i++){
                products.add(i);
            }
            for(int i=0; i<prices.size(); i++){
                prices.add(i);
            
                ArrayList<String> mergePriceProduct = new ArrayList<String>(products.get(i) + prices.get(i));
                
            }
            products() = mergePriceProduct();

        }
        public double get(String products) {
            
            Iterator<String> productIter = products.iterator();
            while (proditer.hasNext()){ 
            System.out.println(prodIter.nextString());
            }

        }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Gee
  • 1
  • 1
    Your method parameters are shadowing the instance variables. Ie you have a list called products, but you also have a variable callled products of type string. and string doesn't have add. if you add this.products.add() it will work – Niclas Lindgren Oct 30 '20 at 07:19
  • 1
    Actually there are a lot of other mistakes in your code as well. but its not so easy to understand what you want to achieve here... It's probably best you study up a bit more on Java. – Niclas Lindgren Oct 30 '20 at 07:20

1 Answers1

0

Your code has couple of issues, but specifically in context of get and add methods, you are facing variable shadowing.

Shadowing refers to the practice in Java programming of using two variables with the same name within scopes that overlap. When you do that, the variable with the higher-level scope is hidden because the variable with lower-level scope overrides it. The higher-level variable is then “shadowed.”

In your case the class level variable name to ArrayList of products and prices is same as your local variable of method put and get. If you want to refer class level variable:

  1. Either rename one of the variable
  2. Or use class level variable with this keyword as this.prices
Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47