Java 如何读取txt文件,并使用其中的双值?

Java 如何读取txt文件,并使用其中的双值?,java,android,text-files,Java,Android,Text Files,我有这样一个.txt文件:它实际上更长 5.4167707e-02 7.4330113e-02 1.3307861e-01 1.1399230e-01 9.7865982e-02 -4.5091141e-02 5.4978066e-02 1.9342541e-01 1.3783929e-01 这是一行字符串。我需要读取文件并在计算中使用这些值。这些值应该类似于输入[i] 以下是我目前的代码: public class MainActivity extends

我有这样一个.txt文件:它实际上更长

  5.4167707e-02   7.4330113e-02   1.3307861e-01   1.1399230e-01   9.7865982e-02  -4.5091141e-02   5.4978066e-02   1.9342541e-01   1.3783929e-01 
这是一行字符串。我需要读取文件并在计算中使用这些值。这些值应该类似于输入[i]

以下是我目前的代码:

public class MainActivity extends AppCompatActivity {

   AudioAnalyzer aa = new AudioAnalyzer();
   BufferedReader brinput = null , brbandpass = null; 
   InputStream isinput = null, isbandpass = null;
   InputStreamReader isrinput = null, isrbandpass = null;

  @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
    }

  public void analyzer (View view) throws FileNotFoundException{
        try {

            isinput = new FileInputStream("/storage/sdcard/input.txt");
            isbandpass = new FileInputStream("/storage/sdcard/bandpass.txt");

            isrinput = new InputStreamReader(isinput);
            isrbandpass = new InputStreamReader(isbandpass);

            brinput = new BufferedReader(isrinput);
            brbandpass = new BufferedReader(isrbandpass);

            //reads to the end of the stream
            int i;
            int f;
            while ((i = brinput.read()) != -1) {
                f = brbandpass.read();
                double input = (double)i; // int to double
                double filtered = (double)f;

              String working = "The filter works!";
              String notworking = "Try again. The filter does not work!";
              TextView t = (TextView) findViewById(R.id.textView);
              if (aa.analyze(in, fil) = true) // Analyze is the methode I want to use the values for
                t.setText(working);
              else
                t.setText(notworking);
              } catch (IOException e) {
            e.printStackTrace();
        }

    }
我无法运行该程序,因为:f处的红线错误可能尚未初始化或编辑。我想将文本文件中的每个值从int改为double类型

double filtered = (double)f;
在“输入”下,过滤后的分析双精度[],双精度[]不能应用于双精度,双精度。编辑如何将其转换为数组以适合该方法

 if (aa.analyze(input, filtered) = true)
编辑 这是我的分析方法:

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;
        boolean work;
        double[] w;
        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++) 
                w[i] = g1[i]-outputFilter[i];
            if (w[i] == 0)
                work = true;
  return work;
  }
和我的过滤器类:

public class IIRFilter {

    private final double[] b;                   
    private final double[] a;                   
    private double[] g;                         //g(n) = output
    private double[] f;                         //f(n) = input

    public IIRFilter(double[] b, double[] a) {
        this.a = a;
        this.b = b;
        g = new double[a.length];
        f = new double[b.length];
        Arrays.fill(g, 0.0);
        Arrays.fill(f, 0.0);

    }

    public double filter(double f0) {
        // change the values' positions in the buffer f
        int s;
        for (s=f.length-1;s>0; s--){
                f[s] = f[s-1];
        }
        f[0] = f0;
        //filter equation
        int n;
        double gt;
        double ht;
        ht = 0.0;
        gt = 0.0;

        for (n = 0; n < b.length; n++) {
            ht += b[n] * f[n];
        }
        for (n = 0; n < a.length; n++) {
            gt -= a[n] * g[n];
        }

        // change the values' positions in the buffer g
        for ( s=g.length; s>0; s--){
                g[s] = g[s-1];
        }
        g[0] = ht+gt;
        return g[0];
    }
}
你可以试试那个代码

分析器

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

首先,=true不会做你想做的事。在if语句中,即使与true进行比较也是多余和不必要的。错误消息非常清晰且不言自明。你能提供更多关于你不理解的细节吗?关于错误,是的;从未初始化。你希望双f做什么?为什么不把f作为一个双精度数组开始呢?既然你还没有展示过AudioAnalyzer,我们就不能告诉你,但是这种分析方法显然接受两个双精度数组,而不仅仅是两个双精度值。实际上你的问题是你没有读取分析过的数据来检查analyzer功能是否正常工作。对于更清晰的代码,您可以使用3个while方法来读取数字列表,第一个是list to list of input.txt,第二个是bandpass.txt,第三个是进行分析,如果我得到了未解析的“readFile”,则使用==on。我猜你指的是“阅读”方法?2无法解析“添加”、“toArray”和“大小”。我应该为此导入哪个util?3对于'List',我得到的类型参数不能是基元typePerfect!无错误。非常感谢。在运行应用程序之前,我仍然需要使用我的分析方法。我的“w”和“work”变量据说尚未初始化。应用程序已启动,但textView未给出任何信息。请检查logcat以查看e上是否有错误。printStackTraceThere对此没有错误。我在错误中只找到了这个:找不到从方法android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering引用的类“android.graphics.drawable.RippleDrawable”
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;
     }
}