Java 在android中解析需要2个键作为输入的文本文件

Java 在android中解析需要2个键作为输入的文本文件,java,android,parsing,Java,Android,Parsing,我有一个包含以下数据的文本文件 其中第一行表示压力,第一列表示温度 用户将输入压力和温度。例如:如果压力为10,温度为25,则输出应为685.2529 txt文件 0.1 1 5 10 30 50 100 -150 -3411.0221 -17133.5640 -10319.7063 -3497.8667 -86.8310 -216.7636

我有一个包含以下数据的文本文件 其中第一行表示压力,第一列表示温度

用户将输入压力和温度。例如:如果压力为10,温度为25,则输出应为685.2529

txt文件

            0.1         1           5           10          30          50          100
-150    -3411.0221  -17133.5640 -10319.7063 -3497.8667  -86.8310    -216.7636   -5992.794714
-125    -2325.2480  -11679.7207 -7034.8053  -2384.4488  -59.1915    -147.7649   -4085.207758
-100    -1528.9398  -7679.8648  -4625.6546  -1567.8667  -38.9207    -97.1611    -2686.180959
-75     -912.3368   -4582.6679  -2760.1839  -935.5649   -23.2244    -57.9772    -1602.876555
-50     -414.9822   -2084.4558  -1255.4873  -425.5477   -10.5638    -26.3713    -729.0786431
-25     0.0000      0.0000      0.0000      0.0000      0.0000      0.0000      0
0       355.9246    1787.8091   1076.8142   364.9864    9.0604      22.6183     625.3207382
25      668.2396    3356.5673   2021.6920   685.2529    17.0107     42.4653     1174.024214
50      947.2608    4758.0910   2865.8428   971.3781    24.1135     60.1965     1664.234178
75      1200.8310   6031.7742   3632.9942   1231.4042   30.5684     76.3104     2109.729457
100     1434.4221   7205.1022   4339.7006   1470.9425   36.5146     91.1547     2520.123568
125     5.0236      25.2337     15.1985     5.1515      0.1279      0.3192      8.825988908
当有两个键作为输入(即压力和温度)时,如何解析它 我正在使用android studio 3.6和java作为编程语言

使用以下代码从资产文件夹读取文本文件

 try {
                        reader = new BufferedReader(
                                new InputStreamReader(getAssets().open("compressibility.txt"), "UTF-8"));

                        // do reading, usually loop until end of file reading
                        String mLine;

                        while ((mLine = reader.readLine()) != null) {
                           // tv.setText(mLine);
                           **// parse and process data**

                        }
                      //  tv.setText(file_full);
                    } catch (IOException e) {
                        //log the exception
                    } finally {
                        if (reader != null) {
                            try {
                                reader.close();
                            } catch (IOException e) {
                                //log the exception
                            }
                        }
                    }

下面给出了如何使用Java进行控制台输入:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        String[] pressure = null;
        String[] temperature = null;
        String[][] data = null;
        int lineCount, index = 0;
        String[] tokens;
        Scanner file = null;
        String line;
        try {
            // Count the no. of lines in the files
            lineCount = (int) Files.lines(Paths.get("file.txt")).count();

            // The size temperature[] = no. of lines - 1 because 1st line is for pressure
            // data
            temperature = new String[lineCount - 1];

            file = new Scanner(new File("file.txt"));
            if (file.hasNextLine()) {
                // Read the first line (pressure data)
                pressure = file.nextLine().split("\\s+");// Split the line on space(s)
            }

            // The no. of rows in data[][] = no. of lines - 1 because 1st line is for
            // pressure data
            data = new String[lineCount - 1][pressure.length];

            // Drop pressure[0] from pressure[] as there is no data there
            pressure = Arrays.copyOfRange(pressure, 1, pressure.length);

            // Read lines until the file is exhausted
            while (file.hasNextLine()) {
                line = file.nextLine();
                tokens = line.split("\\s+");
                temperature[index] = tokens[0];// The first data in the line is temperature

                // Drop tokens[0] from token[] as it has already been stored into temperature[]
                // and store the remaining part of token[] into data[index]
                data[index] = Arrays.copyOfRange(tokens, 1, tokens.length);
                index++;
            }
        } catch (FileNotFoundException e) {
            System.out.println("Unable to read data.");
        } catch (IOException e) {
            System.out.println("Unable to read data.");
        }
        file.close();// Close the scanner for file
        Scanner in = new Scanner(System.in);// Create a scanner for input from console
        System.out.print("Enter the value of pressure: ");
        String p = in.nextLine();
        System.out.print("Enter the value of temperature: ");
        String t = in.nextLine();
        int pIndex = indexOf(p, pressure);// Search for p in pressure[]
        int tIndex = indexOf(t, temperature);// Search for t in temperature[]
        if (pIndex != -1 && tIndex != -1) {
            System.out.println("The required data is: " + data[tIndex][pIndex]);
        } else {
            System.out.println("Data not found.");
        }
    }

    static int indexOf(String value, String[] array) {
        for (int i = 0; i < array.length; i++) {
            if (value.equals(array[i])) {
                return i;// Return index if value is found in array
            }
        }
        return -1;// Return -1 if value is not found in array
    }
}

如果有任何疑问/问题,请随时发表评论。

可以将linecount设置为13行吗?当然,如果您已经知道有13行,可以这样做。System.out.println(“所需的数据是:“+data[tIndex][pIndex-1]);”?这是正确的?根据代码,正确的代码应该是
System.out.println(“所需的数据是:“+data[tIndex][pIndex])
System.out.println没有问题(“需要的数据是:“+data[tIndex][pIndex-1])语法,但它可能会给您错误的结果。有什么理由这样做吗?pressure=Arrays.copyOfRange(pressure,1,pressure.length-1);压力。长度出来是6,而它应该是7
Enter the value of pressure: 10
Enter the value of temperature: 25
The required data is: 685.2529