尝试从文本文件中读取,从数据创建数组然后打印数组。文本文件如下所示
GameSave
4
7583
2946
2946
0586
代码理论上应该查找文本“GameSave”然后读取数组大小的下一行,然后以下行是数组数据。它编译并且在运行代码时没有错误,但它不会向屏幕打印任何内容。任何帮助都会很棒。
import java.util.*;
import java.io.*;
public class LoadArray
{
public static void main ( String[] args ) throws IOException
{
String fileName = "saveGame.txt";
Scanner kb = new Scanner(new File(fileName));
String gameSave = "GameSave";
BufferedReader input = new BufferedReader(new FileReader(fileName));
String in = input.readLine().trim();
String length = input.readLine().trim();
int[][] array = new int[Integer.parseInt(length)][Integer.parseInt(length)];
if (in == gameSave)
{
in = input.readLine();
String[] temp = in.split("");
for (int i = 0; i < array.length; i++)
{
for (int p = 0; p < array.length; p++)
{
array[i][p] = Integer.parseInt(temp[i]);
System.out.print(array[i][p]);
}
System.out.println();
in = input.readLine();
temp = in.split("");
}
}
}}
答案 0 :(得分:0)
这是预期的吗
public class Test {
public static void main(String[] args) throws IOException {
String fileName = "d:\\saveGame.txt";
Scanner kb = new Scanner(new File(fileName));
String gameSave = "GameSave";
BufferedReader input = new BufferedReader(new FileReader(fileName));
String in = input.readLine();
String[] temp;
int[] array = new int[0];
if (in.contains(gameSave)) {
temp = in.split(" ");
array = new int[temp.length - 1];
int j = 0;
for (int i = 1 ; i < temp.length ; i++) {
array[j] = Integer.parseInt(temp[i]);
j++;
}
}
for (int i = 0 ; i < array.length ; i++) {
System.out.print("\n" + array[i]);
}
}
}
<强>输出强>
7583
2946
2946
586