我使用BlueJ
该程序编译正确,但当我尝试执行它时,它说
java.lang.NumberFormatException: empty string (in sun.misc.FLoating Decimal)
at java.lang.Double.parseDouble(Double.java:538)
at Employee.accept(Employee.java:12)
at Employee.main(Employee.java:26)
import java.io.*;
class Employee {
String name;
double gross;
double basic;
char grade;
int age;
public void accept() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter name,age,grade and basic");
name = br.readLine();
age = Integer.parseInt(br.readLine());
grade = (char) br.read();
basic = Double.parseDouble(br.readLine());
double da = 0.25 * basic;
double hra = 0.15 * basic;
double pf = 0.0833 * basic;
double gross = basic + da + hra;
double net = gross - pf;
}
public void display() throws IOException {
System.out.println("Age : " + age + "Grade : " + grade + "Basic : " + basic + "Gross pay : " + gross);
}
public static void main(String args[]) throws IOException {
Employee obj = new Employee();
obj.accept();
obj.display();
}
}
答案 0 :(得分:0)
正如我上面提到的那样,在你的代码中,你要解析它的字符串是不可解析的,因为字符串格式不正确或字符串为空。正如我在你的代码示例和上面的例外中看到的,你已经明确地提到你的字符串是空的,所以在这里你必须检查你的字符串是空的还是在转换之前为null然后只执行类型转换。我给你代码示例请检查它可能对你有所帮助:
import java.io.*;
class Employee {
String name;
double gross;
double basic;
char grade;
int age;
public void accept() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter name,age,grade and basic");
name = br.readLine();
String ageStr = br.readLine();
if(ageStr != null || ageStr != 0)
age = Integer.parseInt();
else
// do your else logic
grade = (char) br.read();
String basicStr = br.readLine();
if(basicStr != null || basicStr != "")
basic = Double.parseDouble(basicStr);
else
// do your else logic
double da = 0.25 * basic;
double hra = 0.15 * basic;
double pf = 0.0833 * basic;
double gross = basic + da + hra;
double net = gross - pf;
}
public void display() throws IOException {
System.out.println("Age : " + age + "Grade : " + grade + "Basic : " + basic + "Gross pay : " + gross);
}
public static void main(String args[]) throws IOException {
Employee obj = new Employee();
obj.accept();
obj.display();
}
}
我希望你能找到解决方案。