Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.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 没有这样的元素异常扫描程序 Scanner s=新扫描仪(新文件(“src/mail_列表”); 而(s.hasNextLine()){ 字符串line1=s.nextLine(); if(第1行开始使用(“用户”)){ line1=s.nextLine(); 而(!(line1=s.nextLine()).isEmpty()){ 字符串arr[]=line1.split(“,4); 添加(新用户(arr[0]、arr[1]、arr[2]、arr[3]); } } if(第1行开始使用(“列表”)){ line1=s.nextLine(); 而(!(line1=s.nextLine()).isEmpty()){//exception here 字符串arr1[]=line1.split(“,2”); ArrayList tmp=新的ArrayList(); StringTokenizer st=新的StringTokenizer(arr1[1]); 而(st.hasMoreTokens()){ tmp.add(st.nextToken()); } 添加(新列表((arr1[0]),tmp)); } } } /*-测试文件启动*/ 关键词:用户 用户名1姓名1姓氏1电子邮件1 用户名2姓名2姓氏2电子邮件2 关键词:列表 列出\u name username1 username2。。。 /*-测试文件结束*/_Java_Nosuchelementexception - Fatal编程技术网

Java 没有这样的元素异常扫描程序 Scanner s=新扫描仪(新文件(“src/mail_列表”); 而(s.hasNextLine()){ 字符串line1=s.nextLine(); if(第1行开始使用(“用户”)){ line1=s.nextLine(); 而(!(line1=s.nextLine()).isEmpty()){ 字符串arr[]=line1.split(“,4); 添加(新用户(arr[0]、arr[1]、arr[2]、arr[3]); } } if(第1行开始使用(“列表”)){ line1=s.nextLine(); 而(!(line1=s.nextLine()).isEmpty()){//exception here 字符串arr1[]=line1.split(“,2”); ArrayList tmp=新的ArrayList(); StringTokenizer st=新的StringTokenizer(arr1[1]); 而(st.hasMoreTokens()){ tmp.add(st.nextToken()); } 添加(新列表((arr1[0]),tmp)); } } } /*-测试文件启动*/ 关键词:用户 用户名1姓名1姓氏1电子邮件1 用户名2姓名2姓氏2电子邮件2 关键词:列表 列出\u name username1 username2。。。 /*-测试文件结束*/

Java 没有这样的元素异常扫描程序 Scanner s=新扫描仪(新文件(“src/mail_列表”); 而(s.hasNextLine()){ 字符串line1=s.nextLine(); if(第1行开始使用(“用户”)){ line1=s.nextLine(); 而(!(line1=s.nextLine()).isEmpty()){ 字符串arr[]=line1.split(“,4); 添加(新用户(arr[0]、arr[1]、arr[2]、arr[3]); } } if(第1行开始使用(“列表”)){ line1=s.nextLine(); 而(!(line1=s.nextLine()).isEmpty()){//exception here 字符串arr1[]=line1.split(“,2”); ArrayList tmp=新的ArrayList(); StringTokenizer st=新的StringTokenizer(arr1[1]); 而(st.hasMoreTokens()){ tmp.add(st.nextToken()); } 添加(新列表((arr1[0]),tmp)); } } } /*-测试文件启动*/ 关键词:用户 用户名1姓名1姓氏1电子邮件1 用户名2姓名2姓氏2电子邮件2 关键词:列表 列出\u name username1 username2。。。 /*-测试文件结束*/,java,nosuchelementexception,Java,Nosuchelementexception,我使用上面的代码是为了从上面的testfile模式中排序。基本上,这意味着如果我遇到关键字“用户”,我必须添加关于用户的上述信息 我在代码中标记了异常出现的位置。有什么办法对付它吗?去吧!(line1=s.nextLine())!=null,不为空,因为它无法读取空行。来自: 抛出: NoTouchElementException-如果未找到行 String line1 = s.nextLine(); if (line1.startsWith("Users")){ lin

我使用上面的代码是为了从上面的testfile模式中排序。基本上,这意味着如果我遇到关键字“用户”,我必须添加关于用户的上述信息

我在代码中标记了异常出现的位置。有什么办法对付它吗?

去吧!(line1=s.nextLine())!=null,不为空,因为它无法读取空行。

来自:

抛出:
NoTouchElementException
-如果未找到行

String line1 = s.nextLine();
    if (line1.startsWith("Users")){
        line1 = s.nextLine();
您有以下代码:

Scanner s = new Scanner(new File("src/mail_list"));    
while (s.hasNextLine()){
        String line1 = s.nextLine();
        if (line1.startsWith("Users")){
            line1 = s.nextLine();
            while (!(line1 = s.nextLine()).isEmpty()) {
                String arr[] = line1.split(" ", 4);
                users.add(new User(arr[0],arr[1],arr[2],arr[3]));
            }
        }
        if (line1.startsWith("Lists")){
            line1 = s.nextLine();
            while (!(line1 = s.nextLine()).isEmpty()) { //exception here
                String arr1[] = line1.split(" ", 2);
                ArrayList<String> tmp = new ArrayList<String>();
                StringTokenizer st = new StringTokenizer(arr1[1]);
                while (st.hasMoreTokens()) {
                    tmp.add(st.nextToken());
                }
                list.add(new List((arr1[0]), tmp));
            }
        }
    }

/*-testfile-start*/
Keyword: Users
username1 Name1 Surname1 email1
username2 Name2 Surname2 email2

Keyword: Lists
list_name username1 username2 ...
/*-testfile-end*/

在使用
Scanner\nextLine
之前,确保确认有一行要读取。相应地处理异常。

您调用了两次
nextLine()
,但只检查了一次
hasNextLine()

while (s.hasNextLine()) {
    //checked if there's line to read
    String line1 = s.nextLine();
    if (line1.startsWith("Users")) {
        //not checked if there's line to read
        line1 = s.nextLine();
        //not checked here either
        while (!(line1 = s.nextLine()).isEmpty()) {
            String arr[] = line1.split(" ", 4);
            users.add(new User(arr[0],arr[1],arr[2],arr[3]));
        }
    }
    //similar in code below...
}
这意味着您在不知道是否有下一行的情况下获取下一行,如果没有,则抛出异常。

如您所见,当找不到任何行时,此方法抛出NoTouchElementException

String line1 = s.nextLine();
    if (line1.startsWith("Users")){
        line1 = s.nextLine();
if(第1行开始使用(“列表”)){

line1=s.nextLine();//我发现了一个愚蠢的解决方案。我只是在最后一行后面添加了一个“dummy”字符2行。它是有效的。这不是一个完美的解决方案,但因为测试文件不应该被任何人看到,所以我现在就接受它。。。
感谢大家在这45分钟里与我一起集思广益。

什么异常?请在线程“main”中发布堆栈跟踪。异常java.util.NoSuchElementException:找不到行。很明显,请调试代码以了解找不到行的原因。我正在调试…如果我发现了问题,我会关闭线程…如果
扫描仪
读取空行,它将返回空字符串
“”
,而不是
null
。没有空行。这就是为什么我在while循环之前执行了(line1=s.nextLine();)操作。它确保它转到检查应该开始的下一行。我不是在比较。我是在分配s.nextLine()的值到要在loopoops中使用的第1行抱歉,错误为:P无论如何,您如何知道您还有更多行?使用此-->!(line1=s.nextLine()).isEmpty()您真的确定它在其他语句中有效吗?如果nextLine()找不到任何行,它应该抛出一个NosTouchElementException,正如您在API.positive的imagen中看到的那样。每次使用-->(line1=s.nextLine())输入while语句之前,我都会检查该行是否为空。isEmpty()