如果用户输入的电子邮件地址中不包含“ @”符号,则必须使用if / else语句退出程序。这是我这部分的代码:
=QUERY({
IFERROR('1'!A2:C, {"","",""});
IFERROR('2'!A2:C, {"","",""});
IFERROR('3'!A2:C, {"","",""});
IFERROR('4'!A2:C, {"","",""});
IFERROR('5'!A2:C, {"","",""});
IFERROR('6'!A2:C, {"","",""});
IFERROR('7'!A2:C, {"","",""});
IFERROR('8'!A2:C, {"","",""});
IFERROR('9'!A2:C, {"","",""});
IFERROR('10'!A2:C, {"","",""});
IFERROR('11'!A2:C, {"","",""});
IFERROR('12'!A2:C, {"","",""});
IFERROR('13'!A2:C, {"","",""});
IFERROR('14'!A2:C, {"","",""});
IFERROR('15'!A2:C, {"","",""});
IFERROR('16'!A2:C, {"","",""});
IFERROR('17'!A2:C, {"","",""});
IFERROR('18'!A2:C, {"","",""});
IFERROR('19'!A2:C, {"","",""});
IFERROR('20'!A2:C, {"","",""})},
"select Col1,sum(Col3)
where Col1 is not null
group by Col1
order by sum(Col3) desc
label sum(Col3)''")
错误:
//prompt user for their email address
System.out.print("\nPlease enter your email address: ");
//read user's input
emailAddress = keyboard.next();
//create username
String username = emailAddress.substring(0, emailAddress.indexOf('@'));
//create username
atSign = emailAddress.lastIndexOf("@");
if(atSign >= 0){
username = emailAddress.substring(0, atSign);
}
else{
System.out.print("You've entered an invalid email address!");
System.out.println("Goodbye!");
}
答案 0 :(得分:3)
问题出在您的电话上
String username = emailAddress.substring(0,emailAddress.indexOf('@'));
如果电子邮件地址中不包含“ @”,则emailAddress.indexOf('@')
返回-1
,表示您正在尝试呼叫emailAddress.substring(0, -1)
。
-1是无效索引,导致StringIndexOutOfBoundsException
。
答案 1 :(得分:3)
String username = emailAddress.substring(0, emailAddress.indexOf('@'));//this will throw exception
该怎么办?
之前创建username
,并将其设置为null
或空字符串。然后检查该符号是否存在。
String username = null;
// or String username = "";
//create username
atSign = emailAddress.lastIndexOf("@");
if(atSign >= 0){
username = emailAddress.substring(0, atSign);
}
else{
System.out.print("You've entered an invalid email address!");
System.out.println("Goodbye!");
// use return to stop code here if in a method(better choice) or
// if in the main() uncomment the code below
// System.exit(0);
}