从 txt 文件读取数据的问题
Issue with reading data from a txt file
我正在从文本文件中读取大部分数据。这一次,我碰巧在从 .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
我有一些其他的 slegs 数组和其他整数范围,我没有任何问题。我会很感激你的建议。
我在你的数据文件中没有看到四个空格,但错误消息是 'For input string: "0 0"'
我猜你的文件里有一个表格。您在编辑器中看不到它,但它可能已转换为四个空格。
尝试更换您的
split(" ");
到
split(""\s+""); \ Thanks for the comment, I did not think about several whitespace characters.
"\s+" 也会理解几个空格和表格。
希望对您有所帮助。
我正在从文本文件中读取大部分数据。这一次,我碰巧在从 .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
我有一些其他的 slegs 数组和其他整数范围,我没有任何问题。我会很感激你的建议。
我在你的数据文件中没有看到四个空格,但错误消息是 'For input string: "0 0"' 我猜你的文件里有一个表格。您在编辑器中看不到它,但它可能已转换为四个空格。 尝试更换您的
split(" ");
到
split(""\s+""); \ Thanks for the comment, I did not think about several whitespace characters.
"\s+" 也会理解几个空格和表格。 希望对您有所帮助。