gpt4 book ai didi

java - 将文本从文件读取到变量中

转载 作者:行者123 更新时间:2023-12-02 10:19:18 24 4
gpt4 key购买 nike

我有一个格式如下的文本文件:

 2 
d e
2
1 0 e
0 1 d

我想要一个名为 size 的变量,它等于第一个数字 4。我还需要一个变量 num 来保存第三行 (3)。我想将第二行 d e 读入数组。我还想将最后两行读入两个数组 - 一个用于字符,另一个用于整数。我将在此处列出我的变量,以便更容易阅读:

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

关于java - 将文本从文件读取到变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54451128/

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