好吧,我正在尝试读取如下所示的文本文件:
FTFFFTTFFTFT
第3054章
第4674章
……等
当我阅读它时,所有内容都可以编译并完美运行,将所有内容放入数组中,如下所示:
学号[0] = 3054
学生ID[1] = 4674
……等
studentAnswers[0] = FTFFFTTFFTFT
studentAnswers[1] = FTFTFFTTTFTF
但是,如果 studentID 有前导零或尾随零,当我用 System.out.println(); 打印它时,它会删除前导零和尾随零!我觉得这很简单,我需要复制数组或其他东西。谢谢:)
下面是我的代码:
public static String[] getData() throws IOException {
int total = 0;
int[] studentID = new int[127];
String[] studentAnswers = new String[127];
String line = reader.readLine();
String answerKey = line;
StringTokenizer tokens;
while((line = reader.readLine()) != null) {
tokens = new StringTokenizer(line);
studentID[total] = Integer.parseInt(tokens.nextToken());
studentAnswers[total] = tokens.nextToken();
System.out.println(total + " " +studentID[total]);
total++;
}
return studentAnswers;
}
最佳答案
使用 String
而不是 int
。作为一般规则,使用整数类型进行计算。
关于java - 为什么我的阵列从我正在阅读的文件中删除零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2177247/