[英]Getting an "Operator '&&' cannot be applied to 'boolean', 'int'" error and I'm unsure why
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.我正在编写一种方法来确定是否有两个“课程”对象,其中“课程”object 由课程名称(字符串)、部门(字符串)、代码(整数)、节(字节)和讲师(字符串)组成), 如果对象具有等效值则返回“true”。 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.
但是,在检查原始“课程”object 和新“课程”object 是否相等的方法部分,我收到上述错误。
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.错误被列为在线
if(this.courseName.equals(course.getName()) &&
,但我不确定它是否指的是整个 if 语句。
Thank you!谢谢!
The error is referring to the entire if
statement.错误是指整个
if
语句。 Byte.compare()
returns an int
, which cannot be used with logical operators. Byte.compare()
返回一个int
,它不能与逻辑运算符一起使用。
For primitive byte
values, you can just use ==
:对于原始
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;
}
Also note that you have a NullPointerException
risk in your string comparisons.另请注意,您在字符串比较中存在
NullPointerException
风险。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.