简体   繁体   English

没有此类元素异常,双线扫描仪

[英]No such element exception, dubble line Scanner

I am trying to add two lines from tableLineScanner , which get's it's lines from a Scanner called tableScanner . 我正在尝试从tableLineScanner添加两行,这就是来自名为tableScannerScanner的行。

Anyhow, I get these errors 无论如何,我得到这些错误

    at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1416)
at Database.main(Database.java:77)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

But as in the code bellow, I can't understand why I get this error. 但是正如下面的代码所示,我不明白为什么会收到此错误。 Since I check if their is two lines available and there by not engage. 由于我检查它们是否有两条线可用,因此没有啮合。

if((tableLineScanner.hasNextLine()&&(tableLineScanner.hasNextLine()))){  //Checks if it exists two available lines.
  fieldInput = (tableLineScanner.next() + " " + (tableLineScanner.next().replaceAll("[()]", ""))+",");
  tableFields.add(fieldInput);
  System.out.println("ADDING: " + fieldInput + " to tableField array list");
}

Please help :( 请帮忙 :(

The method Scanner#hasNextLine() just tells you if there is one more line available. Scanner#hasNextLine()只是告诉您是否还有一行可用。 If you call it twice, it will still just tell you, if there is ONE more line available. 如果您拨打两次,它仍然会告诉您是否还有一条线路可用。 You can call it n times, but it will just tell you about the next line. 您可以调用n次,但是它只会告诉您下一行。

Call Scanner#nextLine() and then you can test again if there is another line available. 呼叫Scanner#nextLine() ,然后您可以再次测试是否有另一行可用。

Always do it that way: 始终这样做:

  1. Test if more data is available 测试是否有更多数据
  2. Read data 读取数据
  3. Test if more data is available 测试是否有更多数据
  4. ... ...

if (tableLineScanner.hasNextLine()) {
    String firstLine = tableLineScanner.nextLine();

    if (tableLineScanner.hasNextLine()) {
        String secondLine = tableLineScanner.nextLine().replaceAll(...);

        tableFields.add(firstLine + secondLine);
    } else {
        // No-second-line error handling goes here
    }
} else {
    // No-first-line error handling goes here (if any)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM