Java 从驱动程序类捕获负数和字母输入异常
我正在尝试实现一个try-catch块来捕获负数和字符作为工资输入 我在基类、驱动程序类、子类中尝试了t-c块,但无法让它捕获这个错误的输入 我试着使用一个双变量来确保工资/收入输入实际上是错误的 同样,在打印e1实例时,第一个字符串值firstName始终打印空值。为什么会这样,它使用调试器检查了它,并且它正确地读取了值,但没有打印它 谢谢你的帮助 //fulltime讲师.javaJava 从驱动程序类捕获负数和字母输入异常,java,Java,我正在尝试实现一个try-catch块来捕获负数和字符作为工资输入 我在基类、驱动程序类、子类中尝试了t-c块,但无法让它捕获这个错误的输入 我试着使用一个双变量来确保工资/收入输入实际上是错误的 同样,在打印e1实例时,第一个字符串值firstName始终打印空值。为什么会这样,它使用调试器检查了它,并且它正确地读取了值,但没有打印它 谢谢你的帮助 //fulltime讲师.java package Employee; public class FullTimeInstructor exte
package Employee;
public class FullTimeInstructor extends Employee {
private double weeklySalary;
public FullTimeInstructor(String firstName, String lastName, String taxFileNumber, double weeklySalary) {
super(firstName, lastName, taxFileNumber);
this.weeklySalary = weeklySalary;
if (weeklySalary < 0) {
throw new IllegalArgumentException("The salary cannot be a negative or a letter");
} else if (weeklySalary != Double.NaN)
/*tried this also...
If (((Object)weeklySalary).getClass().getName()!="java.lang.Double")*/
{
throw new InputMismatchException("The salary cannot be a letter");
}
}
public double getWeeklySalary() {
return weeklySalary;
}
public void setWeeklySalary(double weeklySalary) {
this.weeklySalary = weeklySalary;
}
@Override
public String toString() {
return firstName + " " + lastName + " " + taxFileNumber + " " + weeklySalary + " " +
Earnings();
}
public double Earnings(){
return weeklySalary;
}
}
//java.java
package Employee;
import java.util.InputMismatchException;
public class EarningsTesting {
public static void main(String[] args) throws Exception {
try {
FullTimeInstructor e1 = new FullTimeInstructor("Jane", "Smythe", "456DEF", 29000);
System.out.println(e1);
FullTimeInstructor e2 = new FullTimeInstructor("Janet", "Smith", "987TYR", 20000);
System.out.println(e2);
PartTimeInstructor e3 = new PartTimeInstructor("Alan", "Peters", "345AER", 25.0, 5.0, 20.0, 10.0);
System.out.println(e3);
PartTimeInstructor e4 = new PartTimeInstructor("Boris", "Johnson", "765PER", 25.0, 7.0, 20.0, 11.0);
System.out.println(e4);
SessionalInstructor e5 = new SessionalInstructor("Jeff", "Leyland", "555POI", 20.0, 5.0);
System.out.println(e5);
SessionalInstructor e6 = new SessionalInstructor("Jasmine", "Turner", "856YTE", 20.0, 6.0);
System.out.println(e6);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
} catch (InputMismatchException e) {
System.out.println(e.getMessage());
}
}
}
Try-catch结构用于捕获代码中抛出的异常。如果要生成异常,请在构造函数中引发异常:
if (weeklySalary < 0) {
throw new IllegalArgumentException("The salary of "+ e1.getName() +" is negative");
}
您需要清楚地思考以下内容: 构造employee对象时,是否希望即使weeklySalary为负数也能成功构造它?否则,当构造函数方法为负时,应该在该方法中引发异常。 当您调用收益方法时,是否希望此方法验证每周收入?如果是这样,您应该在收益方法中进行验证,并在异常为负值时抛出异常。
对于第二个问题,您的员工构造函数中有一个输入错误。行'this.lastName=firstName;'应该是'this.firstName=firstName;'名字问题是因为基类构造函数错误。lastName=firstName;应该是这样。firstName=firstName;你没有用你的if语句做任何事情?您检查条件,然后用一个按钮关闭;因此,即使条件为真,它也不会起任何作用。如果this.weeklySalary<0;-这条线没什么用。也许您想在这里抛出一个异常来处理输入为负的情况?然后在测试工具中捕获它?谢谢,这已经解决了我的问题,捕获的第二个异常是char/non-double,但是,这是抛出运行时异常不可编译的源代码-错误的ctor sym类型:。我已经修改了代码来显示我在做什么。在这种情况下不需要检查类型。Java是一种类型化语言,只要不使用强制转换,输入的类型就应该遵守声明的函数类型double。但是如果您确实使用了强制转换,那么您可以使用instanceof更容易地检查类型。关于您的编译异常,这应该是另一个问题的主题。但它看起来像是一个与编辑相关的问题在谷歌上搜索了你的异常并发现:
if (weeklySalary < 0) {
throw new IllegalArgumentException("The salary of "+ e1.getName() +" is negative");
}
try {
FullTimeInstructor e1 = new FullTimeInstructor(someName, otherName, someId, negativeSalary);
} catch (IllegalArgumentException e) {
// do something here with the invalid arguments
}