无法打印从txt文件读取的数组

尝试从文本文件读取,从数据创建一个数组,然后打印该数组。文本文件如下所示

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("");
}
}



}}
安库·辛哈尔(Ankur Singhal)

这是预期的吗

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章