【问题标题】:read the first integer of the first line of a txt file in Java读取Java中txt文件第一行的第一个整数
【发布时间】:2019-02-25 09:11:26
【问题描述】:

我被困在这里...我有一个 txt 文件,其中有几个 (int) 二维数组,由空格分隔。每个二维数组之前的第一行是数组的维度。 (二维数组平方:#rows = #columns = dim)

4

1 2 3 4

5 6 7 8

1 2 3 4

5 6 7 8

在这个阶段,我希望能够在第一行读取数组的维度 (dim =4)。将文件中的二维数组放入二维数组并显示

如何获得第一行的整数?有什么见解吗? 这是我到目前为止所拥有的:

Scanner scan = new Scanner(new File(fileInput));

dim= 4; //this value should be read from the first line

        array = new int[4][4];
        while (scan.hasNext()) {
            for (int row = 0; row < dim; row++) {
                for (int column = 0; column < dim; column++) {
                    array[row][column] = scan.nextInt();
                    System.out.print(array[row][column]);
                }
                System.out.println();
            }

        }
        scan.close();

        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                System.out.print(array[i][j]); 
            }
            System.out.println();

【问题讨论】:

  • ??你知道如何从连续行而不是第一行的文本中读取整数吗?
  • 这是正确的!当我尝试输入第一个值 int FirstVal =scan.NextInt() 时,下一行的第一个整数不会被读取!
  • 显然这没有任何意义。发布MCVE

标签: java input io


【解决方案1】:

这是一个可能的解决方案。

String fileInput = "input.txt";
Scanner scan = new Scanner(new File(fileInput));
int dim = 1; 
int[][] array = null;
int counter = 0; 
while (scan.hasNext()) 
{
    if (counter++ == 0)
    {
        dim = scan.nextInt();
        array = new int[dim][dim];
        continue;
    }
    int i = (counter-2)/dim ;
    int j = (counter-2)%dim ; 
    array[i][j] = scan.nextInt();

    if (i == dim - 1  && j == dim - 1 )
    {
        counter = 0;
        for (i = 0; i < dim; i++) 
        {
            for (j = 0; j < dim; j++) 
            {
                System.out.print(array[i][j]); 
            }
            System.out.println();
        }
    }
}

scan.close();

我用你的输入测试了它

4

1 2 3 4

5 6 7 8

1 2 3 4

5 6 7 8

输出是

1 2 3 4

5 6 7 8

1 2 3 4

5 6 7 8

然后我用这个输入测试了它:

4

1 2 3 4

5 6 7 8

1 2 3 4

5 6 7 8

5

0 1 2 3 4

5 6 7 8 9

0 1 2 3 4

5 6 7 8 9

0 1 2 3 4

输出是

1234

5678

1234

5678

01234

56789

01234

56789

01234

【讨论】:

    【解决方案2】:

    我相信这就是你想做的事?

    这个程序不是手动为dim赋值,而是从文件中读取dim

    int dim = scan.nextInt();
    

    这是完整的代码。

    public class Main2 {
        public static void main(String[] args) throws FileNotFoundException {
    
            File fileInput = new File("file.txt");
    
            Scanner scan = new Scanner(fileInput);
    
            int dim = scan.nextInt(); //this value should be read from the first line
    
            int[][] array = new int[dim][dim];
    
            while (scan.hasNext()) {
                for (int row = 0; row < dim; row++) {
                    for (int column = 0; column < dim; column++) {
                        array[row][column] = scan.nextInt();
                    }
                }
    
            }
            scan.close();
    
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    System.out.print(array[i][j]);
                }
                System.out.println();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-02-12
      • 2021-06-22
      • 2016-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多