将二维字符串数组转换为二维双精度数组
Converting a 2d String array to a 2d double array
我正在做一个项目,我需要在二维数组中的值上设置一定的精度(0.00 或 0.000)。二维数组是 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");
}
}
提前致谢。
尝试初始化你的数组double[][] voorlopig
可能是这样的:
double[][] voorlopig = new double[sheet.length][sheet.length];
你得到 NullPointerException
因为数组 voorlopig
是 double[][] voorlopig = null;
double[][] voorlopig = null;
voorlopig[i][j] = Double.parseDouble(sheet[i][j]);
voorlopig
设置为null
,你会得到一个NullPointerException
。使用前先初始化voorlopig
。
我正在做一个项目,我需要在二维数组中的值上设置一定的精度(0.00 或 0.000)。二维数组是 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");
}
}
提前致谢。
尝试初始化你的数组double[][] voorlopig
可能是这样的:
double[][] voorlopig = new double[sheet.length][sheet.length];
你得到 NullPointerException
因为数组 voorlopig
是 double[][] voorlopig = null;
double[][] voorlopig = null;
voorlopig[i][j] = Double.parseDouble(sheet[i][j]);
voorlopig
设置为null
,你会得到一个NullPointerException
。使用前先初始化voorlopig
。