[Solved] How to read txt file, and use the double values in it?


You can try that code

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class MainActivity extends AppCompatActivity {

    AudioAnalyzer aa = new AudioAnalyzer();
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.textView);
    }

    public void analyzer (View view) throws FileNotFoundException{
        try {
            double[] input = readFile("/storage/sdcard/input.txt");
            double[] analyzed = readFile("/storage/sdcard/bandpass.txt");

            isWorking(aa.analyze(input, analyzed));
        catch (IOException e) {
                e.printStackTrace();
        }

    }
    private void isWorking(boolean working){
        if(working)
            textView.setText("The filter works!");
        else
            textView.setText("Try again. The filter does not work!");           
    }
    public double[] readFile(String fname) throws FileNotFoundException {


    File file = new File(fname); 
        Scanner scanner = new Scanner(new FileInputStream(file));
        List<Double> collection = new ArrayList<Double>();

        while (scanner.hasNextDouble()){
            double number = scanner.nextDouble();
            collection.add(number);
        }

        scanner.close();
        return toPrimitive(collection.toArray(new Double[collection.size()]));
    }   
    public double[] toPrimitive(Double[] array) {
        if (array == null) {
            return null;
        } else if (array.length == 0) {
            return null;
        }
        final double[] result = new double[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = array[i].doubleValue();
        }
        return result;
    }
}

Analyzer

public class AudioAnalyzer {
    private IIRFilter bandpass, lowpass;
    final double[] b1 = {0.0286, 0, -0.0859, 0, 0.0859, 0, -0.0286}; 
    final double[] a1 = {-3.9320, 6.8170, -6.7455, 4.0453, -1.3889, 0.2120};

    public AudioAnalyzer() {
        bandpass = new IIRFilter(b1, a1);
    }

    public boolean analyze(double[] inputFilter, double[] outputFilter) {
        int i;
        double g1[] = new double[inputFilter.length];

        for (i = 0; i < inputFilter.length; i++) {
            double f1 = inputFilter[i];
            g1[i] = outputFilter[i]-(bandpass.filter(f1));

        for (i = 0; i < g1.length; i++){
            if (( g1[i]-outputFilter[i] != 0))
                return false;
        }


         return true;
     }
}

6

solved How to read txt file, and use the double values in it?