Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java字符串索引超出电子邮件项目的范围_Java_Substring_Indexoutofboundsexception - Fatal编程技术网

Java字符串索引超出电子邮件项目的范围

Java字符串索引超出电子邮件项目的范围,java,substring,indexoutofboundsexception,Java,Substring,Indexoutofboundsexception,我正在为一个初学者编码类编写代码,但在为我的电子邮件验证项目生成子字符串时遇到了麻烦。不寻找正则表达式,只是非常简单的初级编码 package email; import java.util.Scanner; /** * Input: An email address of your choosing * Output: If the email is valid, return, "Valid" If invalid, return "Invalid." * An input of "

我正在为一个初学者编码类编写代码,但在为我的电子邮件验证项目生成子字符串时遇到了麻烦。不寻找正则表达式,只是非常简单的初级编码

package email;
import java.util.Scanner;

/**
 * Input: An email address of your choosing
 * Output: If the email is valid, return, "Valid" If invalid, return "Invalid."
 * An input of "connoro@iastate.edu" would return valid because of all the requirements an address should have is present
 */

public class EMail {

    public static void main(String[] args) {

        System.out.print("Enter an email address: ");//input prompt
        String bad = "Email is invalid";//shows if email is invalid
        String good = "Email is valid";//shows if email is valid
        Scanner In = new Scanner(System.in);//Reads input from user
        String email = In.next();//read the input into a String variable using In as the Scanner
        int atIndex = email.indexOf('@');//shows location of '@' character
        int lastDotIndex = email.lastIndexOf('.');//shows location of the last'.' in email String
        int dotIndex = email.indexOf('.');//shows the location of the character of '.'
        String location = email.substring(atIndex, -1);

        if((atIndex == -1) ||(dotIndex == -1)){//If the input email does not have '@' or "." it is invalid
            System.out.println(bad);
        }
        else if(atIndex == 0){//if the character'@' is the first character
            System.out.println(bad);
        }
        else if(dotIndex == email.length()-1){//if the '.' is the last character the email is invalid
            System.out.println(bad);
        }
        else if(lastDotIndex < atIndex){//if the last "." is before the '@' symbol the email is invalid
            System.out.println(bad);
        }
        else if(email.lastIndexOf('.') < email.indexOf('.')){//if the first '.' is the last character the email is invalid
            System.out.println(bad);

        }
        else if((location.length()== -1)|| location.length() <= 0){//If there is no string between the '@' char & the last '.'
            System.out.println(bad);
        }
        else{
            System.out.println(good);
            System.out.println(location);
            }  
    }

}
打包邮件;
导入java.util.Scanner;
/**
*输入:您选择的电子邮件地址
*输出:如果电子邮件有效,返回“有效”,如果无效,返回“无效”
*输入“connoro@iastate.edu“将返回有效,因为地址应满足所有要求
*/
公共类电子邮件{
公共静态void main(字符串[]args){
System.out.print(“输入电子邮件地址:”;//输入提示
String bad=“电子邮件无效”;//显示电子邮件是否无效
String good=“电子邮件有效”;//显示电子邮件是否有效
扫描仪输入=新扫描仪(System.In);//读取用户输入
String email=In.next();//使用In作为扫描程序将输入读入字符串变量
int atIndex=email.indexOf('@');//显示'@'字符的位置
int lastDotIndex=email.lastIndexOf('.');//显示电子邮件字符串中最后一个'.'的位置
int dotIndex=email.indexOf('.');//显示'.'字符的位置
字符串位置=email.substring(atIndex,-1);
如果((atIndex==-1)| |(dotIndex==-1)){//如果输入电子邮件没有“@”或“.”,则无效
系统输出打印项次(坏);
}
else如果(atIndex==0){//如果字符'@'是第一个字符
系统输出打印项次(坏);
}
else如果(dotIndex==email.length()-1){//如果“.”是电子邮件无效的最后一个字符
系统输出打印项次(坏);
}
如果(lastDotIndex否则如果((location.length()=-1)| | location.length()您必须检查电子邮件是否包含@符号,并且不允许继续,因为这意味着验证已经失败

int atIndex = email.indexOf('@');
if (atIndex < 0) {
  System.out.println(bad);
  return;
}
int-atIndex=email.indexOf('@');
如果(指数<0){
系统输出打印项次(坏);
回来
}

如果atIndex为负值,它将抛出(正如akos.borads所说)

这将始终引发错误:
String location=email.substring(atIndex,-1);


也许你想要
String location=email.substring(atIndex+1,email.length());

不要描述你的异常,在你的问题中包含整个堆栈跟踪的确切文本。这很有效,谢谢!你能解释一下+1和email.length()的原因吗?引入位置时?a索引指向at字符的索引,因为要跳过它,需要再移动一个位置。然后将所有字符移动到email.lenght()-1的最后一个位置,但子字符串第二个参数是第一个跳过的字符(不是最后一个),因此第一个跳过的字符位于位置(email.lenght()-1)+1=email.lenght()