找不到行-Java

找不到行-Java,java,Java,我正在输入一个txt文件,这里是一个简短的版本 10 "Alexander McCall Smith" "No. 1 Ladies' Detective Agency" 我运行以下代码: Scanner in = new Scanner(new File(newFile + ".txt")); int size = in.nextInt(); String inputLine = in.nextLine(); 大小最终为10,但inputLine最终什么也接收不到。我得到了错误 Exce

我正在输入一个txt文件,这里是一个简短的版本

10  
"Alexander McCall Smith" "No. 1 Ladies' Detective Agency"
我运行以下代码:

Scanner in = new Scanner(new File(newFile + ".txt"));
int size = in.nextInt();
String inputLine = in.nextLine();
大小最终为10,但inputLine最终什么也接收不到。我得到了错误

Exception in thread "main" java.util.NoSuchElementException: No line found.
我去了调试器,它说一个位置为(-1,-1)的字符串是java试图插入到
inputLine中的字符串。
我不知道为什么,我知道10之后有50多行文本。我在.next()中运行了
,效果很好。有人知道为什么吗

我还运行以下代码:

inputLine.trim();
int posn = inputLine.indexOf('\"');
int nextPosn = inputLine.indexOf('\"',posn + 1);
String author = inputLine.substring(posn, nextPosn);

安德鲁·李说得对。调用
nextInt
不会占用该行,因此您仍然在第一行,即上面有“10”的那一行

如果您连续两次调用
nextLine
,您将得到“Alexander”行


(我不知道你从哪里得到了一个
NoTouchElementException
。它一定是来自你程序中的其他地方。)

安德鲁·李说得对。调用
nextInt
不会占用该行,因此您仍然在第一行,即上面有“10”的那一行

如果您连续两次调用
nextLine
,您将得到“Alexander”行


(我不知道您从哪里得到了一个
NoTouchElementException
。它一定是来自您程序中的其他地方。)

我想知道您是否没有正确处理线尾令牌。通常,如果使用Scanner#next####()(nextLine除外),并且在用户按enter键时到达行尾标记,如果不处理行尾标记,则会阻止Scanner对象正常工作。要解决此问题,请在需要处理此令牌时调用Scanner#nextLine()

下面的帖子有更详细的解释。

我想知道您是否没有正确处理线尾令牌。通常,如果使用Scanner#next####()(nextLine除外),并且在用户按enter键时到达行尾标记,如果不处理行尾标记,则会阻止Scanner对象正常工作。要解决此问题,请在需要处理此令牌时调用Scanner#nextLine()

下面的帖子有更详细的解释。

如果文件中有多个条目,则应在每行上循环并分别处理它们。在代码搜索“/”符号时,如果未找到该符号,则索引将为-1,因此您也应处理这些情况,下面是一个示例:

public static void main(String[] args){
    Scanner in = null;
    int lines;
    int size;
    String inputLine;
    int posn, nextPosn;

    try {
        in = new Scanner(new File("testFile.txt"));
    }
    catch(IOException e){
        System.err.print("File read error: "+e.getMessage());
        System.exit(1);
    }

    lines = 0;
    // Iterate over the lines in the file
    while(in.hasNext()){
        inputLine = in.nextLine();
        System.out.println(inputLine);
        inputLine = inputLine.trim();
        if(lines == 0){
            // If you know that next line is an integer
            size = Integer.parseInt(inputLine);
        }
        posn = inputLine.indexOf('\"');
        nextPosn = inputLine.indexOf('\"',posn + 1);
        if(posn >=0 && nextPosn >=0) {
            String author = inputLine.substring(posn, nextPosn);
        }
        lines++;
    }
}

如果文件中有多个条目,则应在每行上循环并分别处理它们。在代码搜索“/”符号时,如果未找到该符号,则索引将为-1,因此您也应处理这些情况,下面是一个示例:

public static void main(String[] args){
    Scanner in = null;
    int lines;
    int size;
    String inputLine;
    int posn, nextPosn;

    try {
        in = new Scanner(new File("testFile.txt"));
    }
    catch(IOException e){
        System.err.print("File read error: "+e.getMessage());
        System.exit(1);
    }

    lines = 0;
    // Iterate over the lines in the file
    while(in.hasNext()){
        inputLine = in.nextLine();
        System.out.println(inputLine);
        inputLine = inputLine.trim();
        if(lines == 0){
            // If you know that next line is an integer
            size = Integer.parseInt(inputLine);
        }
        posn = inputLine.indexOf('\"');
        nextPosn = inputLine.indexOf('\"',posn + 1);
        if(posn >=0 && nextPosn >=0) {
            String author = inputLine.substring(posn, nextPosn);
        }
        lines++;
    }
}

您是否正在使用
hasNextLine
进行检查?当我这样做时,它会跳过它。我知道事实上还有下一行,因为这只是我100行中的第2行。我真的没有任何线索,这是我第一次犯这样的错误,我简直目瞪口呆。正如@AndrewLi所说的,先和hasNextLine联系一会儿,或者。。然后继续
inputLine
实际上是一个换行符
nextLine
接收整数,但仍有一个换行符,该换行符通过
nextLine
指定给
inputLine
。您能否提供更多信息,因为当前代码不会引发异常。此外,如果您在正在阅读的文本后添加换行符,会发生什么?您是否正在使用
hasNextLine
进行检查?我这样做时,它会跳过它。我知道事实上还有下一行,因为这只是我100行中的第2行。我真的没有任何线索,这是我第一次犯这样的错误,我简直目瞪口呆。正如@AndrewLi所说的,先和hasNextLine联系一会儿,或者。。然后继续
inputLine
实际上是一个换行符
nextLine
接收整数,但仍有一个换行符,该换行符通过
nextLine
指定给
inputLine
。您能提供更多信息吗,因为当前代码不会引发异常。另外,如果您在正在读取的文本后放置换行符,会发生什么?对,因此我创建了一个“伪”字符串并执行了.nextLine(),这之后解决了我的整个问题。非常感谢,我想那只是一个巨大的脑屁。不用担心,处理文件常常令人沮丧。@TeenCoder不需要假字符串。调用
nextLine
将使扫描器在换行符上前进,因此它应该可以工作。对,因此我创建了一个“伪”字符串并执行了.nextLine(),它随后解决了我的整个问题。非常感谢,我想那只是一个巨大的脑屁。不用担心,处理文件常常令人沮丧。@TeenCoder不需要假字符串。调用
nextLine
将使扫描仪在换行线上前进,因此它应该可以工作。