获取一个”Operator ‘&&’ cannot be applied to ‘boolean’, ‘int'”错误,我不太确定原因。

huangapple go评论78阅读模式
英文:

Getting an "Operator '&&' cannot be applied to 'boolean', 'int'" error and I'm unsure why

问题

我正在编写一个方法,用于确定两个“Course”对象是否相等。其中一个“Course”对象由courseName(字符串)、department(字符串)、code(整数)、section(字节)和instructor(字符串)组成,如果这两个对象具有相同的值,则返回“true”。然而,在检查原始“Course”对象和新的“Course”对象是否相等的方法部分,我遇到了上述错误。

参考代码:

public boolean equals(Course obj){
    if(obj instanceof Course){

        Course course = (Course)obj;

        if(this.courseName.equals(course.getCourseName()) &&
                this.department.equals(course.getDepartment()) &&
                this.code == course.getCode() &&
                this.section == course.getSection() &&
                this.instructor.equals(course.getInstructor()))
            return true;
    }
    return false;
}

错误显示在行if(this.courseName.equals(course.getName()) &&,但我不确定它是否指的是整个if语句。

谢谢!

英文:

I'm writing a method that is to determine if two "Course" objects, where a "Course" object is comprised of courseName (String), department (String), code (int), section (byte), and instructor (String), and returns "true" if the objects have equivalent values. However, in the portion of the method that checks if they original "Course" object and the new "Course" object are equal, I am getting the above error.

Code for reference:

public boolean equals(Course obj){
    if(obj instanceof Course){

        Course c = (Course)obj;

        if(this.courseName.equals(course.getName()) &&
                this.department.equals(course.getDepartment()) &&
                (this.code==course.getCode()) &&
                (Byte.compare(this.section, course.getSection())) &&
                this.instructor.equals(course.getInstructor()))
            return true;
    }
    return false;
}

Error is listed as being on the line if(this.courseName.equals(course.getName()) &&, but I'm unsure if it's referring to the entire if statement.

Thank you!

答案1

得分: 3

错误是指整个 if 语句。Byte.compare() 返回一个 int,不能与逻辑运算符一起使用。

对于原始的 byte 值,您可以直接使用 ==

if(this.courseName.equals(course.getName()) &&
        this.department.equals(course.getDepartment()) &&
        this.code == course.getCode() &&
        this.section == course.getSection() &&
        this.instructor.equals(course.getInstructor())) {

    return true;
}

还要注意在字符串比较中存在 NullPointerException 风险。

英文:

The error is referring to the entire if statement. Byte.compare() returns an int, which cannot be used with logical operators.

For primitive byte values, you can just use ==:

if(this.courseName.equals(course.getName()) &&
        this.department.equals(course.getDepartment()) &&
        this.code == course.getCode() &&
        this.section == course.getSection() &&
        this.instructor.equals(course.getInstructor())) {
   
    return true;
}

Also note that you have a NullPointerException risk in your string comparisons.

huangapple
  • 本文由 发表于 2020年9月2日 12:49:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63698882.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定