Steve Steve - 1 month ago 15
Java Question

Runtime error - java.lang.NumberFormatException: empty String

I have written this code for image analysis:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ImageAnalysis {

private double[][] filter;
private double[][] Im;
private int k;
private int n;
private int m;

public ImageAnalysis(String filename) {

BufferedReader br = null;

try {

String sCurrentLine;

br = new BufferedReader(new FileReader(filename));
sCurrentLine = br.readLine();
String[] val = sCurrentLine.split(" ");
// extracting the row and column from file first line
int row = Integer.parseInt(val[0]);
int col = Integer.parseInt(val[1]);

n = row;
m = col;

Im = new double[row][col];
int i = 0;
// reading the file line by line
while ((sCurrentLine = br.readLine()) != null) {
val = sCurrentLine.split(" ");
// System.out.println(val.length+" "+i);
// store the values into two dimensional array
for (int j = 0; j < col; j++) {
Im[i][j] = Double.parseDouble(val[j]);
}
i++;
}

} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}

/*
*
* filter[0][0]=0; filter[0][1]=-1/8; filter[0][2]=0;
*
* filter[1][0]=-1/8; filter[1][1]=1/2; filter[1][2]=-1/8;
*
* filter[2][0]=0; filter[2][1]=-1/8; filter[2][2]=0;
*/

}

// this method will apply filter on image file
public double[][] runFilter(String filterfilename) {
BufferedReader br = null;

try {

String sCurrentLine;

br = new BufferedReader(new FileReader(filterfilename));
sCurrentLine = br.readLine();
String[] val = sCurrentLine.split(" ");
// extracting k value from file first line
k = Integer.parseInt(val[0]);
// applying the formula 2*k+1 to calculate row and col of matrix
int row = 2 * k + 1, col = 2 * k + 1;
// make array
filter = new double[row][col];
int i = 0;
// reading the file line by line
while ((sCurrentLine = br.readLine()) != null) {
val = sCurrentLine.split(" ");
for (int j = 0; j < col; j++) {
filter[i][j] = Double.parseDouble(val[j]);
}
i++;
}

} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}

// calculate the row and col for filtered image
int row = n - 2 * k;
int col = m - 2 * k;
// make filtered image 2D array
double[][] filteredImage = new double[row][col];
// filled the vvalue in filtered array by applying formula
for (int a = 0; a < row; a++) {
for (int b = 0; b < col; b++) {
double sum = 0;
for (int i = -k; i <= k; i++) {
for (int j = -k; j <= k; j++) {
sum += filter[i + k][j + k] * Im[a + i + k][b + j + k];
}
}
filteredImage[a][b] = sum;
// System.out.println(sum);
}
}
return filteredImage;
}

}


It seems to work fine with .ftr files, but when I try with .ipi, I get the following runtime error:

Exception in thread "main" java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at assignment2.ImageAnalysis.<init>(ImageAnalysis.java:44)
at ImageAnalysis_test_visible_04.main(ImageAnalysis_test_visible_04.java:3)


The .ipi file is:

18 20
1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
1.0 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0 -1.0 -1.0 -1.0 -1.0 -1.0 -1.0


The program uses real values between -1 and 1 where -1 means "no light at all" = black, 0 corresponds to 50% gray and 1 is white.

Answer

In your while loop I would do

val = sCurrentLine.split("\\s+");

just in case there are more than one spaces