【问题标题】:Reading contents from a text file and storing them into an array从文本文件中读取内容并将它们存储到数组中
【发布时间】:2015-10-06 23:18:44
【问题描述】:

我有一个包含这些值的 5x5 表格的文本文档

5   5
39  95  99  56  41
88  8   1   48  75
3   58  13  54  80
92  72  74  25  86
30  38  3   21  2

我必须将它们添加到一个数组中,并显示最小值(即 1)并告诉最小值的位置(第 1 行第 2 列)。

public static void main(String[] args)
{
    Scanner input;
    File fileIn = new File("src/array2d/array2dtest1.txt");
    System.out.println(fileIn.getAbsolutePath());

    int[][] array = new int[5][5];
    for(int row = 0;row<array.length;row++) {int[] column = array[row];
    {
        for(int columnIndex = 0; columnIndex<column.length; columnIndex++);
    }


    }
    try
    {

        input = new Scanner(fileIn);
    }
    catch (FileNotFoundException e)
    {

        System.out.println(fileIn.getName() + " is not found.");
        return;
    }
    input.close();
}

}

【问题讨论】:

    标签: java arrays loops text


    【解决方案1】:

    此代码实际上将您的输入存储到一个数组中。

    public static void main(String[] args) {  
                Scanner input;
                File fileIn = new File("src/array2d/array2dtest1.txt");
                System.out.println(fileIn.getAbsolutePath());
    
                int[][] array = new int[5][5];
    
                try
                {
    
                    input = new Scanner(fileIn);
                    String values = input.nextLine();
                    String[] value = values.split("\\s+");
                    int index = 0;
                    for(int row = 0;row < 5;row++) 
                    {   index = row;
                        for(int col = 0 ; col < 5; col++){
                                array[row][col] = Integer.parseInt(value[index*5 + col]);
                        } 
                    }
                }
                catch (FileNotFoundException e)
                {
    
                    System.out.println(fileIn.getName() + " is not found.");
                    return;
                }
                input.close();
            }
    

    【讨论】:

      【解决方案2】:

      使用来自@vikasn91 的答案,我对其进行了一些编辑以正确地将值分配给数组,找到最小的数字及其在数组中的位置:

          try {
      
              input = new Scanner(fileIn);
              int lowestCol = 0;
              int lowestRow = 0;
              int lowest = 0;
      
              for (int row = 0; row < 5; row++) {
                  String values = input.nextLine();
                  String[] value = values.split("\\s+");
                  for (int col = 0; col < 5; col++) {
                      array[row][col] = Integer.parseInt(value[col]);
      
                      if (row == 0 && col == 0) {
                          lowest = array[row][col];
                      } else if (array[row][col] < lowest) {
                          lowestCol = col;
                          lowestRow = row;
                          lowest = array[lowestRow][lowestCol];
                      }
                  }
              }
      
              System.out.println("Lowest number: " + lowest);
              System.out.println("Found in row: " + lowestRow + ", col: " + lowestCol);
          } catch (FileNotFoundException e) {
      
              System.out.println(fileIn.getName() + " is not found.");
              return;
          }
          input.close();
      

      【讨论】:

        【解决方案3】:
        public static void main(String[] args)
        {
            Scanner input;
            File fileIn = new File("array2dtest1.txt");
            System.out.println(fileIn.getAbsolutePath());
            try
            {
                input = new Scanner(fileIn);
                int row = input.nextInt();
                int column = input.nextInt();
                int min = Integer.MAX_VALUE;
                int val;
                int minR=0,minC=0;
        
                for(int i=0;i<row;i++){
                    for(int j=0;j<column;j++){
                        val = input.nextInt();
                        if(val<min){
                            min = val;
                            minR = i;
                            minC = j;
                        }
                    }
                }
                System.out.println("Min Value is " + min + "\nat position (" + minR + "," + minC + ")" );
            }
            catch (FileNotFoundException e)
            {
                System.out.println(fileIn.getName() + " is not found.");
                return;
            }
            input.close();
        }
        

        【讨论】:

          【解决方案4】:

          如果您使用 Scanner,则无需直接拆分或解析整数。默认分隔符是空格。

          Scanner s = new Scanner(new FileReader("src/array2d/array2dtest1.txt"));
          int numRows = s.nextInt();
          int numCols = s.nextInt();
          int[][] array = new int[numRows][numCols];
          int least = Integer.MAX_VALUE;
          int leastRow = -1;
          int leastCol = -1;
          for(int r = 0; r < numRows; r++) {
              for(int c = 0; c < numCols; c++) {
                  if((array[r][c] = s.nextInt()) < least) {
                      leastRow = r;
                      leastCol = c;
                  }
              }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2021-06-09
            • 1970-01-01
            • 1970-01-01
            • 2021-12-08
            • 2017-07-14
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多