-2

I know this question is simple but i cant figure it out. My ArrayList "elemente" contains a number of double values. I have written the sum()-Method to add up all the values which works fine on its own. The normalize()- Method intends to divide the individual double values of "elemente" by the sum() and add them into the new ArrayList "normalized".

When trying to compile this code i get the error : " error: cannot find symbol: method sum()".

I have been trying to figure this out for about 2 hours now, so any help you can give me would be greatly appreciated :)

import java.io.*;
import java.util.*;
class Measurement {
   public Measurement() {
       this.elemente = new ArrayList<Double>();
   }
   public void setArray(String s){
       try {
           FileInputStream fis = new FileInputStream(s);
           InputStreamReader isr = new InputStreamReader(fis);
           BufferedReader br = new BufferedReader(isr);
           String eingabe;
           
           eingabe = br.readLine();
           while (eingabe != null) {
               elemente.add(Double.parseDouble(eingabe));
               eingabe = br.readLine();
               }
       }
       catch (Exception e) {
           System.out.println("Hi");
       }
   }
   public double sum(){
       double result = 0.0;
       Iterator<Double> e = elemente.iterator();
       while (e.hasNext()) {
           result += e.next();
       }
       return result;
   }
   public double min(){
       return Collections.min(elemente);
       }
   public double max(){
       return Collections.max(elemente);
       }
   public double mean(){
       double result = 0.0;
       double count = 0.0;
       Iterator<Double> e = elemente.iterator();
       while (e.hasNext()) {
           result += e.next();
           count += 1;
       }
       return result/count;
   }
   public ArrayList normalize(){
       ArrayList<Double> normalized = new ArrayList<Double>();
       Iterator<Double> e = elemente.iterator();
       while (e.hasNext()) {
           normalized.add(e.next()/elemente.sum()); // I cant call the prevously definded Method sum() here. 
       }
       return normalized;
   }

   private ArrayList<Double> elemente;
}
Sargnagel
  • 19
  • 4
  • 5
    `sum()` exists on class `Measurement ` but you're calling it on `elemente` - an `ArrayList`. Just use `e.next() / sum()`. – Rob Spoor May 27 '22 at 10:51
  • 2
    A few tips for better code readability: 1) Put all your variables at the top of your class body. 2) You can use `new ArrayList<>()` without explicitly saying that it should be an `ArrayList`, java knows that by the declaration already. 3) You can use `for (Double next : elemente) {/* code */}` instead of all the `Iterator`-stuff, it compiles to the same thing. Try using an IDE, IntelliJ automatically tells me those things. – Cactusroot May 27 '22 at 11:06

0 Answers0