将双打存储到2D阵列中

时间:2019-03-14 03:14:59

标签: java

我无法将文本文件中的数字存储到二维数组中。第一个数字代表行数,第二个数字代表列数。文本文件中的值为:

3 3 
13.3 14 
9.9 1.7
0.343 2 6.44234
0.1234 798

它已经成功地将前2个数字存储为行和列,但是在运行后给了我以下输出:

3
3
Error discovered at 0,0: java.lang.ArrayIndexOutOfBoundsException: 0
Error discovered at 0,1: java.lang.ArrayIndexOutOfBoundsException: 0
Error discovered at 0,2: java.lang.ArrayIndexOutOfBoundsException: 0
Error discovered at 1,0: java.lang.ArrayIndexOutOfBoundsException: 1
Error discovered at 1,1: java.lang.ArrayIndexOutOfBoundsException: 1
Error discovered at 1,2: java.lang.ArrayIndexOutOfBoundsException: 1
Error discovered at 2,0: java.lang.ArrayIndexOutOfBoundsException: 2
Error discovered at 2,1: java.lang.ArrayIndexOutOfBoundsException: 2
Error discovered at 2,2: java.lang.ArrayIndexOutOfBoundsException: 2

我不确定是什么问题,但这是我正在使用的代码:

public class twoDArray 
{

    public static void main(String[] args) throws IOException
    {
    String filename = JOptionPane.showInputDialog("Please enter the file name: ");
    File file = new File(filename);
    try 
    {
        Scanner inputFile = new Scanner(file);
        inputFile.close();
    } 
    catch (FileNotFoundException e) 
    {
        JOptionPane.showMessageDialog(null, "File does not exist", "error", 1);
        System.exit(0);
    } 
    int ROW_SIZE = 0;
    int COLUMN_SIZE = 0;
    double[][] TEST_ARRAY = new double[ROW_SIZE][COLUMN_SIZE]; 
    loadArray(filename, ROW_SIZE, COLUMN_SIZE, TEST_ARRAY);

public static void loadArray(String file, int ROW_SIZE, int COLUMN_SIZE, double[][] TEST_ARRAY) throws FileNotFoundException{
    Scanner inputFile = new Scanner(new File(file));
    while(inputFile.hasNextInt()) {
        ROW_SIZE = inputFile.nextInt();
        COLUMN_SIZE = inputFile.nextInt();
    }
    System.out.println(ROW_SIZE);
    System.out.println(COLUMN_SIZE);
     for (int i = 0; i < ROW_SIZE; i++) {
            for (int j = 0; j <  COLUMN_SIZE; j++) {
                try {
                    if (inputFile.hasNextDouble()){
                        TEST_ARRAY[i][j] = inputFile.nextDouble();
                    }
                } 
                catch (Exception e) 
                {
                    System.err.println("Error discovered at " + i + "," + j + ": " + e);
                }
            }
     }

    inputFile.close();

}
}

任何帮助或指导将不胜感激,因为我已经花了7多个小时进行调试并试图找出解决方案。

1 个答案:

答案 0 :(得分:1)

library(dplyr)

df1 <- df %>%
          group_by(names, reps) %>%
          mutate(start = lag(time, default = 0), 
                 end = time) 

bind_rows(df1, df1 %>%
                 group_by(names, reps) %>%
                 summarise(start = last(time), 
                            end = Inf, 
                            value = sum(value))) %>%
                 select(-time) %>%
                 arrange(names, reps) 


#  names reps  value start   end
#   <fct> <fct> <int> <dbl> <dbl>
# 1 Han   A         2     0     1
# 2 Han   A         2     1     2
# 3 Han   A         1     2     3
# 4 Han   A         1     3     4
# 5 Han   A         3     4     5
# 6 Han   A         2     5     6
# 7 Han   A         0     6     7
# 8 Han   A         2     7     8
# 9 Han   A         2     8     9
#10 Han   A         5     9    10
#11 Han   A        20    10   Inf
#.....

int ROW_SIZE = 0; int COLUMN_SIZE = 0; double[][] TEST_ARRAY = new double[ROW_SIZE][COLUMN_SIZE]; ROW_SIZE此时均为0。它正在分配一个0x0数组。您需要延迟调用COLUMN_SIZE,直到您阅读了两种尺寸。或者您需要更早阅读尺寸。两者之一。