【问题标题】:catching negative and letter input exception from driver class从驱动程序类中捕获负数和字母输入异常
【发布时间】:2021-06-04 10:53:39
【问题描述】:

我正在尝试实现一个 try-catch 块来捕获负数和字符作为薪水输入。

我已经在基类、驱动类、子类中尝试过 t-c 块,但无法让它捕捉到这个错误输入。

我尝试使用双变量来确保“工资/收入”输入实际上是错误的。

同样在打印 e1 实例时,第一个字符串值“firstName”总是打印 null。为什么会这样,用调试器检查了一下,它正确读取了值但没有打印出来。

感谢您的帮助。

//FullTimeInstructor.java

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;
}
}

//employee.java

package Employee;

abstract class Employee {

public String firstName;
public String lastName;

public String taxFileNumber;

public Employee(String firstName, String lastName,String taxFileNumber) {
  this.firstName = firstName;
  this.lastName = lastName;
  this.taxFileNumber = taxFileNumber;
  }

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getTaxFileNumber() {
    return taxFileNumber;
}

public void setTaxFileNumber(String taxFileNumber) {
    this.taxFileNumber = taxFileNumber;
}


public String toString() {
    return firstName + " " + lastName + " " + taxFileNumber;
}

public abstract double Earnings();
}

//EarningsTesting.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());
    }
}
}

【问题讨论】:

  • 对于您的第二个问题,您的 Employee 构造函数中有一个错字。行'this.lastName = firstName;'应该是 'this.firstName = firstName;'
  • 第一个名字问题是因为基类构造函数错误this.lastName = firstName;应该是this.firstName = firstName;
  • 您没有对您的if 语句做任何事情?您检查条件,然后立即以; 关闭,因此即使条件为真,它也不会执行任何操作。
  • 不完全确定你想用这段代码实现什么if (this.weeklySalary &lt; 0); - 这行没有做任何事情。也许您想在此处抛出一个异常来处理输入为负数的情况?然后在你的测试工具中捕获它?

标签: java


【解决方案1】:

Try-catch 结构用于捕获代码中已经抛出的异常。如果您想生成异常,请在构造函数中抛出异常:

if (weeklySalary < 0) {
    throw new IllegalArgumentException("The salary of "+ e1.getName() +" is negative");
}

然后你可以用 try - catch 块来捕捉它并对其进行操作(例如):

try {
    FullTimeInstructor e1 = new FullTimeInstructor(someName, otherName, someId, negativeSalary);
} catch (IllegalArgumentException e) {
    // do something here with the invalid arguments
}

【讨论】:

  • 谢谢,这解决了我的问题,要捕获的第二个异常是 char/non-double,但是,这是抛出运行时异常“无法编译的源代码 - 错误的 ctor sym 类型: ”。我已经修改了我的代码以显示我在做什么。
  • 这种情况下不需要检查类型。 Java 是一种类型化语言,只要不使用强制转换,输入的类型应该尊重函数声明的类型(double)。但是如果你确实使用了强制转换,那么你可以使用 instanceof 更轻松地检查类型。关于您的编译异常,这应该是另一个问题的主题。但它看起来像是一个与编辑器相关的问题(谷歌搜索你的异常并找到:stackoverflow.com/questions/35114025/…
【解决方案2】:

你需要清楚地思考如下:

  • 当你构造一个employee对象时,你是否希望它在weeklySalary为负数的情况下也能成功构造?如果不是,你应该在构造函数方法中抛出一个异常,当它是负数时。
  • 当您调用 Earnings 方法时,您是否希望该方法验证weeklySalary?如果是这样,您应该在 Earnings 方法中进行验证,并在为负数时抛出异常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-28
    • 2012-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-13
    相关资源
    最近更新 更多