繁体   English   中英

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

[英]parsing text file in android requiring 2 keys as input

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

用户将输入压力和温度。 例如:如果压力: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

当有 2 个键作为输入时如何解析它,即压力和温度我使用 android studio 3.6 和 java 作为编程语言

使用以下代码从 Assets 文件夹中读取文本文件

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

注意: file.txt内容正是您在问题中发布的内容。

示例运行:

Enter the value of pressure: 10
Enter the value of temperature: 25
The required data is: 685.2529

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM