gpt4 book ai didi

java - 从txt文件读取数据的问题

转载 作者:行者123 更新时间:2023-12-02 10:54:11 26 4
gpt4 key购买 nike

我正在从文本文件中读取大部分数据。这次,我在从 .txt 文件读取数据数组时遇到了一些问题。我提供部分代码如下:MWE:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class Model {
public static final int Y = 7;
public static final int K = 42;
public static final int T = 10;

public static class Sleg {
// private int id;
private int i;
private int j;
private double l;

public Sleg(int i, int j, double l) {
// this.id = id;
this.i = i;
this.j = j;
this.l = l;
}

@Override
public String toString() {
return Integer.toString(i) + " " + Integer.toString(j) + " " + Double.toString(l);
}
}


public static void dataGen() {

List<Sleg> slegs = new ArrayList<Sleg>();
File slFile = new File("slFile.txt");

try (BufferedReader reader = new BufferedReader(new FileReader("slFile.txt"))) {


String line;
while ((line = reader.readLine()) != null) {

String[] numbers = line.trim().split(" ");
int i = Integer.parseInt(numbers[0]);
int j = Integer.parseInt(numbers[1]);
double l = Double.parseDouble(numbers[2]);
slegs.add(new Sleg(i, j, l));
}

} catch (FileNotFoundException e) {
System.out.println(slFile.toString() + " does not exist.");
} catch (IOException e) {
// Handle any possible IOExceptions as well...
System.out.println("Unable to read : " + slFile.toString());
}

System.out.print("slegs = [");
for (Sleg s : slegs) {
System.out.print("[" + s + "] ");
}
System.out.println("]");
System.out.println("-----------------------------------------------------------------------------------------------------------------------------------------------");
Zat[][] u = new int [slegs.size()][T];
File zatFile = new File("zat.txt");
try (BufferedReader br = new BufferedReader(new FileReader("zat.txt"))) {
String line;
while ((line = br.readLine()) != null) {
for (int sl = 0; sl < slegs.size(); sl++) {
String[] ln = line.trim().split(" ");
for (int t = 0; t < T; t++) {
Zat[sl][t] = Integer.parseInt(ln[t]);
}
}
}
} catch (FileNotFoundException e) {
System.out.println("Unable to find : " + zatFile.toString());
} catch (IOException e) {
System.out.println("Unable to read : " + zatFile.toString());
}catch (NumberFormatException e) {
System.out.println("not an integer");
}
System.out.print("Zat = [");
for (int sl = 0; sl < slegs.size(); sl++) {
System.out.print("[");
for (int t = 0; t < T; t++) {
System.out.print(" " + Zat[sl][t] + " ");
}
System.out.print("]");
}
System.out.println("]");
System.out.println(
"-----------------------------------------------------------------------------------------------------------------------------------------------");


}
}

当我运行代码时,会弹出一个错误,如下所示:

Exception in thread "main" java.lang.NumberFormatException: For input string: "0    0"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Model.dataGen(Model.java:832)
at SolverMethod.main(SolverMethod.java:9)

sFile文件:

1 2 400
2 5 800
5 7 450
2 3 800
3 6 550
3 4 500
4 5 500
7 5 450
5 4 500
4 3 400
6 3 550
3 2 700
2 1 400
5 2 800

和 zat 文件:

1 0 0 0 1 0 0 0 0 0
1 0 0 0 0 1 0 0 0 0
1 0 0 0 0 0 0 0 0 0
0 1 0 0 1 0 0 0 0 1
0 1 0 0 0 0 0 0 0 0
0 0 1 0 1 0 0 0 1 0
0 0 1 0 0 0 0 0 1 0
0 0 0 1 0 0 1 0 0 0
0 0 0 1 0 1 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1 0
0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 1 1 0 0
0 0 0 0 0 0 1 0 0 1

我有一些其他的雪橇数组和其他整数范围,但没有任何问题。我将不胜感激您的建议。

最佳答案

我在您的数据文件中没有看到四个空格,但错误消息是“对于输入字符串:“0 0””我猜你的文件里有一个表格。您在编辑器中看不到它,但它可能已转换为四个空格。尝试更换您的

split(" ");

split(""\\s+""); \\ Thanks for the comment, I did not think about several whitespace characters.

“\s+”也会理解几个空格和表格。我希望这会有所帮助。

关于java - 从txt文件读取数据的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51913817/

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