将txt文件数据读入double
Reading txt file data into doubles
我正在尝试将文本文件中的数据保存到包含双精度数的数组中。我已经让它适用于整数,但我想添加非整数数据,到目前为止我还想不通。到目前为止,这是我的代码:
public class 主类 {
public static void main(String[] args) throws FileNotFoundException {
List<Double> E0 = Arrays.asList();
List<Double> E1 = Arrays.asList();
List<Double> E2 = Arrays.asList();
List<Double> C = Arrays.asList();
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader("C:\Data.txt"));
String line = reader.readLine();
while (line != null) {
Double lineParts = line.split(",");
E0.add(lineParts[0]);
E1.add(lineParts[1]);
E2.add(lineParts[2]);
C.add(lineParts[3]);
line = reader.readLine();
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(E0);
System.out.println(E1);
System.out.println(E2);
System.out.println(C);
}
}
这是我的数据:
1,1,4,-1
1,2,9,1
1,5,6,1
1,4,5,1
1,6,7,-1
1,1,1,-1
下面的代码显示了如何解析一行并将条目转换为双精度。
import java.util.Arrays;
public class ParseDouble {
public static void main(String[] args) {
String line = "1,2,3,-4";
String[] lineSplit = line.split(",");
double[] values = new double[lineSplit.length];
for (int i = 0; i < lineSplit.length; i++) {
values[i] = Double.parseDouble(lineSplit[i]);
}
System.out.println(Arrays.toString(values)); // [1.0, 2.0, 3.0, -4.0]
}
}
使用您的代码,以下几行应该可以完成工作:
public class MainClass {
public static void main(String[] args) throws FileNotFoundException {
List<Double> E0 = new ArrayList();
List<Double> E1 = new ArrayList();
List<Double> E2 = new ArrayList();
List<Double> C = new ArrayList();
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader("C:\Data.txt"));
String line = reader.readLine();
while (line != null) {
String[] lineParts = line.split(",");
E0.add(Double.parseDouble(lineParts[0]));
E1.add(Double.parseDouble(lineParts[1]));
E2.add(Double.parseDouble(lineParts[2]));
C.add(Double.parseDouble(lineParts[3]));
line = reader.readLine();
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(Arrays.toString(E0.toArray()));
System.out.println(Arrays.toString(E1.toArray()));
System.out.println(Arrays.toString(E2.toArray()));
System.out.println(Arrays.toString(C.toArray()));
}
}
你的问题的完整答案是将静态分离方法转换为完全动态解析方法,没有任何错误,这个答案是最安全的解决方案,不取决于你插入的数据有多长。
您可以借助包含双精度值列表
的列表来实现这一点
您的数据示例:
List<List<Double>> lines = new ArrayList<>();
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader("C:\Data.txt"));
String line = reader.readLine();
while (line != null) {
List<Double> lineList = new ArrayList<>();
String[] lineParts = line.split(",");
for (String linePart : lineParts) {
lineList.add(Double.valueOf(linePart));
}
lines.add(lineList);
line = reader.readLine();
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(lines);
}
此致:)
我正在尝试将文本文件中的数据保存到包含双精度数的数组中。我已经让它适用于整数,但我想添加非整数数据,到目前为止我还想不通。到目前为止,这是我的代码:
public class 主类 {
public static void main(String[] args) throws FileNotFoundException {
List<Double> E0 = Arrays.asList();
List<Double> E1 = Arrays.asList();
List<Double> E2 = Arrays.asList();
List<Double> C = Arrays.asList();
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader("C:\Data.txt"));
String line = reader.readLine();
while (line != null) {
Double lineParts = line.split(",");
E0.add(lineParts[0]);
E1.add(lineParts[1]);
E2.add(lineParts[2]);
C.add(lineParts[3]);
line = reader.readLine();
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(E0);
System.out.println(E1);
System.out.println(E2);
System.out.println(C);
}
}
这是我的数据:
1,1,4,-1
1,2,9,1
1,5,6,1
1,4,5,1
1,6,7,-1
1,1,1,-1
下面的代码显示了如何解析一行并将条目转换为双精度。
import java.util.Arrays;
public class ParseDouble {
public static void main(String[] args) {
String line = "1,2,3,-4";
String[] lineSplit = line.split(",");
double[] values = new double[lineSplit.length];
for (int i = 0; i < lineSplit.length; i++) {
values[i] = Double.parseDouble(lineSplit[i]);
}
System.out.println(Arrays.toString(values)); // [1.0, 2.0, 3.0, -4.0]
}
}
使用您的代码,以下几行应该可以完成工作:
public class MainClass {
public static void main(String[] args) throws FileNotFoundException {
List<Double> E0 = new ArrayList();
List<Double> E1 = new ArrayList();
List<Double> E2 = new ArrayList();
List<Double> C = new ArrayList();
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader("C:\Data.txt"));
String line = reader.readLine();
while (line != null) {
String[] lineParts = line.split(",");
E0.add(Double.parseDouble(lineParts[0]));
E1.add(Double.parseDouble(lineParts[1]));
E2.add(Double.parseDouble(lineParts[2]));
C.add(Double.parseDouble(lineParts[3]));
line = reader.readLine();
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(Arrays.toString(E0.toArray()));
System.out.println(Arrays.toString(E1.toArray()));
System.out.println(Arrays.toString(E2.toArray()));
System.out.println(Arrays.toString(C.toArray()));
}
}
你的问题的完整答案是将静态分离方法转换为完全动态解析方法,没有任何错误,这个答案是最安全的解决方案,不取决于你插入的数据有多长。
您可以借助包含双精度值列表
的列表来实现这一点您的数据示例:
List<List<Double>> lines = new ArrayList<>();
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader("C:\Data.txt"));
String line = reader.readLine();
while (line != null) {
List<Double> lineList = new ArrayList<>();
String[] lineParts = line.split(",");
for (String linePart : lineParts) {
lineList.add(Double.valueOf(linePart));
}
lines.add(lineList);
line = reader.readLine();
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(lines);
}
此致:)