0

I have reviewed all the previous questions with a similar title as this one, however I can't find a solution. All the errors are suggesting that I am not initializing the ArrayList.. Am I not initializing the ArrayList as such with, = new ArrayList<Double> ?

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.PrintWriter;

public class main
{
    public static void main (String[] args) throws FileNotFoundException
    {
        ArrayList<Double> RPM, bCoeffs, filteredRPM = new ArrayList<Double>();
        Scanner RPMFile = new Scanner(new File("RotationSpeed.txt"));

        while(RPMFile.hasNextLine()){
            String line = RPMFile.nextLine();

            Scanner scanner = new Scanner(line);
            scanner.useDelimiter(",");
            while(scanner.hasNextDouble()){
                RPM.add(scanner.nextDouble());
            }
            scanner.close();
        }
        RPMFile.close();

        int windowSize = 10;
        int filterItterations = 1;

        for (int i = 0; i < windowSize; i++){
                double temp = 1/windowSize;
                bCoeffs.add(temp);
        }

        for (int k = 1; k <= filterItterations; k++){
            if (k == 1){
                for (int n = windowSize; n < RPM.size(); n++){
                    int m = 0;
                    double tempYSum = 0;
                    for (int j = 0; j < windowSize; j++){
                        double tempY = (bCoeffs.get(j))*(RPM.get(n-m));
                        tempYSum += tempY;
                        m++;
                    }
                    filteredRPM.add(tempYSum);
                }
            }else{
                int i = 1;
                for (int n = windowSize; n < filteredRPM.size(); n++){
                    int m = 0;
                    double tempYSum = 0;
                    for (int j = 0; j < windowSize; j++){
                        double tempY = (bCoeffs.get(j))*(filteredRPM.get(n-m));
                        tempYSum += tempY;
                        m++;
                    }
                    filteredRPM.set(i, tempYSum);
                    i++;
                }
            }
        }
    }
}

the errors I am receiving are as follows:

main.java:20: error: variable RPM might not have been initialized
                RPM.add(scanner.nextDouble());
                ^
main.java:31: error: variable bCoeffs might not have been initialized
                                bCoeffs.add(temp);
                                ^
main.java:36: error: variable RPM might not have been initialized
                                for (int n = windowSize; n < RPM.size(); n++){
                                                             ^
main.java:40: error: variable bCoeffs might not have been initialized
                                                double tempY = (bCoeffs.get(j))*(RPM.get(n-m));
                                                                ^
main.java:52: error: variable bCoeffs might not have been initialized
                                                double tempY = (bCoeffs.get(j))*(filteredRPM.get(n-m));
                                                                ^
5 errors

2 Answers 2

3

Just assign the variables: you're currently declaring 3 variables, but only assigning a value to the last one.

ArrayList<Double> RPM = new ArrayList<>(), bCoeffs = new ArrayList<>(), filteredRPM = new ArrayList<>();

Note that declaring many variables on the same line is discouraged by some, e.g. it is forbidden by Google's style guide: it is easier to read if you do one per line:

ArrayList<Double> RPM = new ArrayList<>();
ArrayList<Double> bCoeffs = new ArrayList<>();
ArrayList<Double> filteredRPM = new ArrayList<>();
Sign up to request clarification or add additional context in comments.

1 Comment

This suggestion solves the problem. Thank you very much
1

While your first line might look like it initializes all three ArrayLists it basically does the following:

ArrayList<Double> RPM;
ArrayList<Double> bCoeffs;
ArrayList<Double> filteredRPM = new ArrayList<Double>();

If it worked the way you probably intended it though you would create a lot of potential questions that would need clarification. For example: Would all three variables now point to the same memory (in other words to the same ArrayList)? Or do they all point to a different ArrayList.

There is a reason why it is discouraged to initialize several variables in one line as it's quite obfuscating.

The correct way for you to do it would be:

ArrayList<Double> RPM = new ArrayList<>();
ArrayList<Double> bCoeffs = new ArrayList<>();
ArrayList<Double> filteredRPM = new ArrayList<>();

2 Comments

It is perhaps worth noting that even if it were a "really nice and super optimized" to initialize all variables, it wouldn't be clear if the class instance creation expression (new ArrayList<Double>()) would be evaluated once for each variable, or just once and the same value assigned to all three variables.
Oh for sure. The "really nice and super optimized" is probably a bad wording anyhow. I will just rephrase.

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.