When i call this function. numberformatexception occured,, how can i fix it.. help me please
public void read() throws FileNotFoundException, IOException, ClassNotFoundException, ParseException {
FileInputStream fis = null;
ObjectInputStream in = null;
File f = new File("Employee.dat");
if (f.exists()) {
fis = new FileInputStream("Employee.dat");
in = new ObjectInputStream(fis);
String x = (String) in.readObject();
String[] node = x.split("saqib");
for (int i = 0; i < node.length; i++) {
String recrd[] = node[i].split("$");
try {
AddToTail(Integer.parseInt(recrd[0]), recrd[1], recrd[2],
recrd[3], recrd[4], Integer.parseInt(recrd[5]),
recrd[6], recrd[7], recrd[8], recrd[9], recrd[10], recrd[11]);
} catch (NumberFormatException e) {
System.out.println("NumberFormatException");
}
}
in.close();
}
}
There is nothing to fix!
This
NumberFormatException
is the way your program tells you that the input data are not in the format you assumed. Especially, that there is not a number in some part of the input where you expected one.There is no way around considering the case that input data may be invalid.
Integer.parseInt
throwsNumberFormatException
when the input to the method is not a valid number string i.e contains something else than digits. Input should be a string that contains a parsable integer. So make sure that you are passing the proper input to the method to avoid this exception.