【问题标题】:no such element exception scanner没有这样的元素异常扫描器
【发布时间】:2015-04-17 16:00:30
【问题描述】:
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*/

我正在使用上面的代码从上面的测试文件模式中排序。基本上这意味着如果我遇到关键字“用户”,我必须添加有关用户的上述信息。

我在代码中标记了异常出现的位置。关于如何应对它的任何想法?

【问题讨论】:

  • 什么异常?请发布堆栈跟踪。
  • 线程“主”java.util.NoSuchElementException 中的异常:找不到行。
  • 嗯..很清楚了,请调试您的代码以了解为什么找不到行。
  • 我正在调试...如果我发现问题我会关闭线程...

标签: java nosuchelementexception


【解决方案1】:

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

String line1 = s.nextLine();
    if (line1.startsWith("Users")){
        line1 = s.nextLine();

意味着您在不知道是否有下一行的情况下获取下一行,如果没有则抛出异常。

【讨论】:

    【解决方案2】:

    我找到了一个愚蠢的解决方案。我刚刚在最后一行之后添加了一个“虚拟”字符 2 行。它有效。这不是一个完美的解决方案,但由于任何人都不会看到测试文件,所以我现在就接受它...... 感谢所有在这 45 分钟内与我进行头脑风暴的人。

    【讨论】:

      【解决方案3】:

      执行 !(line1 = s.nextLine()) != null,not empty,因为它无法读取空行。

      【讨论】:

      • 如果Scanner 读取一个空行,它将返回一个空字符串"",而不是null
      • 没有空行。这就是为什么我在 while 循环之前执行 (line1 = s.nextLine();) 的原因。它确保它转到应该开始检查的下一行。
      【解决方案4】:

      来自Scanner#nextLine

      抛出:
      NoSuchElementException - 如果没有找到行。

      你有这个代码:

      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...
      }
      

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

      【讨论】:

        【解决方案5】:

        如您所见,此方法在找不到行时抛出 NoSuchElementException。

        if (line1.startsWith("Lists")){
                line1 = s.nextLine(); // <=============== ?
                while (!(line1 = s.nextLine()).isEmpty()) { //exception here
        

        你怎么知道那里和你的评论行中有更多的行?

        【讨论】:

        • 我没有比较。我将 s.nextline() 的值分配给 line1 以在循环内使用
        • 抱歉,读错了:P 无论如何,你怎么知道你有更多的行?
        • with this --> !(line1 = s.nextLine()).isEmpty()
        • 您确定它在其他语句中有效吗?如果 nextLine() 没有找到任何行,它应该抛出 NoSuchElementException,正如您在 API 的图像中看到的那样。
        • 积极。我每次都在使用这个输入while语句之前检查该行是否为空-->!(line1 = s.nextLine()).isEmpty()
        猜你喜欢
        • 2020-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多