java - 将文件中的文本读入变量
问题描述
我有一个格式如下的文本文件:
2
d e
2
1 0 e
0 1 d
我想要一个名为 size 的变量,它等于第一个数字 4。我还需要一个变量 num 来保存第三行 (3)。我想将第二行 de 读入一个数组。我还想将最后两行读入两个数组——一个用于字符,另一个用于整数。我将在这里列出我的变量,以便于阅读:
size = 2 ---> first line
char[] chars = {d, e} ---> second line
num = 2 ---> third line
int[] pos = {1, 0, 0, 1} ---> Lines 4, 5
char[] posLetters = {e, d} ---> Lines 4, 5
在读取文件中的文本之前,我不确定如何声明每个数组的大小。这很棘手,因为另一个文本文件可以读取:
3
a b c
4
0 3 b
2 0 c
1 2 a
0 2 b
第二个文件中的变量将是:
size = 3 ---> first line
char[] chars = {a, b, c} ---> second line
num = 4 ---> third line
int[] pos = {0, 3, 2, 0, 1, 2, 0, 2} ---> Lines 4, 5, 6, 7
char[] posLetters = {b, c, a, b} ---> Lines 4, 5, 6, 7
这是我的代码的第四或第五稿。我撞墙了。我知道如何逐行打印文件,但我不知道如何逐个变量地打印。
try {
File configFile = new File(config + "config.txt");
Scanner configScanner = new Scanner(configFile);
int size = configScanner.nextInt();
int num = configScanner.nextInt();
/*
int[][] board = new int[size][size];
for(int i = 0; i < size-1; i ++) {
for(int j = 0; j < size-1; j++) {
board[i][j] =
configScanner.nextInt();
}
}
*/
} catch (FileNotFoundException e) {
// handle the exception here
System.err.println(e);
e.printStackTrace();
} // try
我收到此错误:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at LatinSqauresGame.printWelcome(LatinSqauresGame.java:59)
at LatinSqauresGame.<init>(LatinSqauresGame.java:13)
at LatinSquaresDriver.main(LatinSquaresDriver.java:6)
我希望在开头列出结果:
size = 2 ---> first line
char[] chars = {d, e} ---> second line
num = 2 ---> third line
int[] pos = {1, 0, 0, 1} ---> Lines 4, 5
char[] posLetters = {e, d} ---> Lines 4, 5
编辑:这是我现在拥有的代码:
int size = variableScan.nextInt();
char[] chars = new char[size];
for (int i = 0; i < size; i++) {
chars[i] = variableScan.next().charAt(0);
}
// Read pos and posLetters arrays
int num = variableScan.nextInt();
// code here to allocate and read pos and posLetters
System.out.println(size);
for (int i = 0; i < size; i++)
System.out.println(chars[i]);
System.out.println(num);
int iter = 0;
while(variableScan.hasNextLine()) {
int[] pos = new int[num*2];
for(int i = 0; i < 2; i++) {
pos[i + (2*iter)] = variableScan.nextInt();
}
char[] posLetters = new char[num];
for (int i = 0; i < 1; i++) {
posLetters[i + iter] = variableScan.next().charAt(0);
}
iter++;
}
与此文本文件配对:
2
a b
2
0 0 a
1 0 b
我得到输出:
2
a
b
2
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at LatinSqauresGame.printWelcome(LatinSqauresGame.java:77)
at LatinSqauresGame.<init>(LatinSqauresGame.java:13)
at LatinSquaresDriver.main(LatinSquaresDriver.java:6)
解决方案
你从调用nextInt()
两次开始,但你的文件只以一个数字开头,所以它当然会在第二次nextInt()
调用中失败。
你的代码应该像这样开始:
// Read chars array
int size = configScanner.nextInt();
char[] chars = new char[size];
for (int i = 0; i < size; i++)
chars[i] = configScanner.next().charAt(0);
// Read pos and posLetters arrays
int num = configScanner.nextInt();
// code here to allocate and read pos and posLetters
推荐阅读
- scala - 如何在 Scala 中过滤地图列表
- next.js - Next.js 静态站点生成自定义文件路径
- javascript - 调用API时如何访问数组中对象的空间属性?
- r - 如何选择使用 GLM 在 R 中创建模型要省略的因子水平
- python - django/wagtail - 当管理面板另有说明时,对象属性显示无?
- javascript - 如何拆分具有多个元素的数组以获取 JavaScript 中的最后一项?
- ionic-framework - Laravel 8 sanctum - angular 10 xsrf 问题
- reactjs - 如何防止多次重新渲染反应
- excel - 根据结果比较 2 列和颜色填充
- scala - 将文件从 Spark 放到本地 FS 中的每个节点上