将2d String数组转换为2d double数组

时间:2015-12-23 09:53:46

标签: java arrays string double

我正在制作一个项目,我需要在二维数组中对值赋予一定的精度(0.00或0.000)。 2d数组是String,我想将其转换为double。但是当我尝试时,我得到一个NullPointerException,我不知道为什么。 之后我想再次将其转换为String数组。

打印方法代码:

public void printSheet() {
    int start = 'a';
    char letter = (char) start;
    //kolomnamen
    for (int i = 0; i < COLUMNS + 1; i++) {
        if (i == 0) {
            System.out.print("   ");
        } else if (WIDTH != 0) {
            String s = "";
            for (int j = 0; j < WIDTH / 2; j++) {
                s += "-";
            }
            s += (char) (letter + i - 1);
            for (int j = 0; j < WIDTH / 2; j++) {
                s += "-";
            }
            System.out.print(s + "\t");
        }
    }

    System.out.println("\n");
    for (int i = 0; i < sheet.length; i++) {
        System.out.print(1 + i + "." + "    ");
        for (int j = 0; j < sheet[i].length; j++) {
             double[][] voorlopig = null;
            voorlopig[i][j] = Double.parseDouble(sheet[i][j]); //<-- problem
            double d = voorlopig[i][j];
           // String s = " " + sheet[i][j];

            System.out.println(voorlopig);
            double thaprez = (precision / precision) + (Math.pow(precision, 10)) - 1;
            d = Math.floor(d * thaprez + .5) / thaprez;
            String s = " " + voorlopig[i][j];

            if (sheet[i][j] == null) {
                System.out.print(s + "\t\t");
            } else if (sheet[i][j].length() < 10) {
                System.out.print(s + "\t");
            } else {
                System.out.print(s);
            }
        }
        System.out.println("\n");
    }
}

提前致谢。

2 个答案:

答案 0 :(得分:0)

尝试初始化数组double[][] voorlopig,如下所示:

  double[][] voorlopig = new double[sheet.length][sheet.length];

您得到NullPointerException因为数组voorlopigdouble[][] voorlopig = null;

答案 1 :(得分:0)

double[][] voorlopig = null;
voorlopig[i][j] = Double.parseDouble(sheet[i][j]);

voorlopig设置为null,您将获得NullPointerException。在使用之前初始化voorlopig