[英]int cannot be converted to charsequence
I am trying to loop through my arraylist and print email addresses that are invalid. 我试图遍历我的arraylist并打印无效的电子邮件地址。 To be invalid, the address does not contain an @ sign or a period.
如果无效,则地址不包含@符号或句点。
I am getting an error, int cannot be converted to charsequence for my method. 我收到一个错误,int无法转换为我的方法的charsequence。
public static void print_invalid_emails(){
for (int i = 0; i < studentList.size(); i++) {
if (studentList.get(i).getEmail().contains('@' + '.')){
System.out.println("Scanning Roster");
}
else {
System.out.println("Invalid email address, " + studentList.get(i).getEmail());
}
}
}
The problem is that Java treats '@' + '.'
问题是Java会对待
'@' + '.'
as an addition of two characters, not as a concatenation of a string. 作为两个字符的添加,而不是字符串的串联。 The result of this addition is UNICODE code point of
@
plus unicode code point of .
这个加法的结果是UNICODE代码点
@
加上unicode代码点.
, expressed as an int
. ,表示为
int
。
If you want to check that the item is missing a .
如果要检查项目是否缺失a
.
or a @
, you should perform these two checks in two separate calls of contains(...)
或者
@
,你应该在两个单独的contains(...)
调用中执行这两个检查
The bug is here: 错误在这里:
if (studentList.get(i).getEmail().contains('@' + '.')){
It should be: 它应该是:
if (studentList.get(i).getEmail().contains("@") || studentList.get(i).getEmail().contains(".")){
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.