扫描仪内的扫描仪错误
当试图扫描一个文本文件,然后扫描文本文件中的每个单独的行来创建一个对象时,我会得到下面的错误。 任何想法如何我可以解决这个问题?
错误: java.util.Scanner.throw上的java.util.NoSuchElementException (MyException.java:111)
while(scanner.hasNextLine()){
    lineOfInput = scanner.nextLine();
    if(lineOfInput.startsWith("#")){
    } else {
        String animalType, species, name;
        Scanner newScanner = new   Scanner(lineOfInput).useDelimiter("s*,s*");
        animalType = newScanner.next();
        System.out.println(animalType);
        species    = newScanner.next();
        name       = newScanner.nextLine();
   }
正如你在评论中所说的那样
“这是一个包含数据行的文本文件,我使用第一台扫描仪在行中读取,然后尝试扫描读入的行中的每个单词以将它们分配为变量,在我扫描前3行后,我希望它阅读动物特定课程的其余数据“
你的第二个扫描器使用错误的分隔符,因此你得到了这个异常。
尝试:
Scanner newScanner = new   Scanner(lineOfInput).useDelimiter("W");
希望这可以帮助。
我认为你可以这样做:
while (scanner.hasNextLine()) {
    lineOfInput = scanner.nextLine();
    if (lineOfInput.startsWith("#")) {
    } else {
        String animalType, species, name;
        String s[] = lineOfInput.split(" ");
        animalType = s[0];
        species = s[1];
        name = s[2];
    }
}
