gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 08:48:42 24 4
gpt4 key购买 nike

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

用户将输入压力和温度。例如:如果压力: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 作为编程语言

使用以下代码从 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

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

关于java - 在android中解析文本文件需要2个键作为输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60916151/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com