java.util.NoSuchElementException:找不到行

发布于 2024-12-02 00:39:29 字数 1329 浏览 0 评论 0原文

当我通过扫描仪读取文件时,我的程序出现运行时异常。

java.util.NoSuchElementException: No line found     
   at java.util.Scanner.nextLine(Unknown Source)    
   at Day1.ReadFile.read(ReadFile.java:49)  
   at Day1.ParseTree.main(ParseTree.java:17) 

我的代码是:

while((str=sc.nextLine())!=null){
    i=0;
    if(str.equals("Locations"))
    {
        size=4;
        t=3;
        str=sc.nextLine();
        str=sc.nextLine();
    }
    if(str.equals("Professions"))
    {
        size=3;
        t=2;
        str=sc.nextLine();
        str=sc.nextLine();
    }
    if(str.equals("Individuals"))
    {
        size=4;
        t=4;
        str=sc.nextLine();
        str=sc.nextLine();
    }

int j=0;
String loc[]=new String[size];
while(j<size){
    beg=0;
    end=str.indexOf(',');
    if(end!=-1){
        tmp=str.substring(beg, end);
        beg=end+2;
    }
    if(end==-1)
    {
        tmp=str.substring(beg);
    }
    if(beg<str.length())
        str=str.substring(beg);
    loc[i]=tmp;
    i++;

    if(i==size ){
        if(t==3)
        {
            location.add(loc);
        }
        if(t==2)
        {
            profession.add(loc);
        }
        if(t==4)
        {
            individual.add(loc);
        }
        i=0;
    }
    j++;
    System.out.print("\n");
}

I got an run time exception in my program while I am reading a file through a Scanner.

java.util.NoSuchElementException: No line found     
   at java.util.Scanner.nextLine(Unknown Source)    
   at Day1.ReadFile.read(ReadFile.java:49)  
   at Day1.ParseTree.main(ParseTree.java:17) 

My code is:

while((str=sc.nextLine())!=null){
    i=0;
    if(str.equals("Locations"))
    {
        size=4;
        t=3;
        str=sc.nextLine();
        str=sc.nextLine();
    }
    if(str.equals("Professions"))
    {
        size=3;
        t=2;
        str=sc.nextLine();
        str=sc.nextLine();
    }
    if(str.equals("Individuals"))
    {
        size=4;
        t=4;
        str=sc.nextLine();
        str=sc.nextLine();
    }

int j=0;
String loc[]=new String[size];
while(j<size){
    beg=0;
    end=str.indexOf(',');
    if(end!=-1){
        tmp=str.substring(beg, end);
        beg=end+2;
    }
    if(end==-1)
    {
        tmp=str.substring(beg);
    }
    if(beg<str.length())
        str=str.substring(beg);
    loc[i]=tmp;
    i++;

    if(i==size ){
        if(t==3)
        {
            location.add(loc);
        }
        if(t==2)
        {
            profession.add(loc);
        }
        if(t==4)
        {
            individual.add(loc);
        }
        i=0;
    }
    j++;
    System.out.print("\n");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

南笙 2024-12-09 00:39:29

使用 Scanner 时,您需要使用 hasNextLine() 检查是否有下一行,

以便循环成为

while(sc.hasNextLine()){
    str=sc.nextLine();
    //...
}

返回 null 的读取器

EOF上 这段代码取决于输入的格式是否正确

with Scanner you need to check if there is a next line with hasNextLine()

so the loop becomes

while(sc.hasNextLine()){
    str=sc.nextLine();
    //...
}

it's readers that return null on EOF

ofcourse in this piece of code this is dependent on whether the input is properly formatted

拔了角的鹿 2024-12-09 00:39:29

我也遇到了这个问题。
就我而言,问题是我在其中一个函数中关闭了扫描仪。

public class Main 
{
    public static void main(String[] args) 
    {
        Scanner menu = new Scanner(System.in);
        boolean exit = new Boolean(false);
        while(!exit) {
            String choose = menu.nextLine();
            Part1 t=new Part1()
            t.start();
            System.out.println("Noooooo Come back!!!"+choose);
        }
        menu.close();
    }
}

public class Part1 extends Thread 
{
    public void run()
    { 
        Scanner s = new Scanner(System.in);
        String st = s.nextLine();
        System.out.print("bllaaaaaaa\n" + st);
        s.close();
    }
}

上面的代码产生了相同的例外,解决方案是仅在主函数中关闭扫描仪一次。

I also encounter with that problem.
In my case the problem was that I closed the Scanner inside one of the funcs..

public class Main 
{
    public static void main(String[] args) 
    {
        Scanner menu = new Scanner(System.in);
        boolean exit = new Boolean(false);
        while(!exit) {
            String choose = menu.nextLine();
            Part1 t=new Part1()
            t.start();
            System.out.println("Noooooo Come back!!!"+choose);
        }
        menu.close();
    }
}

public class Part1 extends Thread 
{
    public void run()
    { 
        Scanner s = new Scanner(System.in);
        String st = s.nextLine();
        System.out.print("bllaaaaaaa\n" + st);
        s.close();
    }
}

The code above made the same exeption, the solution was to close the scanner only once at the main.

爱已欠费 2024-12-09 00:39:29

您正在调用 nextLine() ,并且当没有行时它会抛出异常,正如 javadoc 所描述的那样。它永远不会返回 null

https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html

You're calling nextLine() and it's throwing an exception when there's no line, exactly as the javadoc describes. It will never return null

https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html

清旖 2024-12-09 00:39:29

无论出于何种原因,如果 Scanner 类遇到无法读取的特殊字符,它也会发出相同的异常。除了在每次调用 nextLine() 之前使用 hasNextLine() 方法之外,请确保将正确的编码传递给 Scanner 构造函数,例如:

扫描仪扫描仪 = new Scanner(new FileInputStream(filePath), "UTF-8");

For whatever reason, the Scanner class also issues this same exception if it encounters special characters it cannot read. Beyond using the hasNextLine() method before each call to nextLine(), make sure the correct encoding is passed to the Scanner constructor, e.g.:

Scanner scanner = new Scanner(new FileInputStream(filePath), "UTF-8");

渡你暖光 2024-12-09 00:39:29

您真正的问题是您调用“sc.nextLine()”的次数比行数多

例如,如果您只有个输入行,那么您可以ONLY调用“sc.nextLine()”次。

每次调用“sc.nextLine()”时,都会消耗一个输入行。如果您调用“sc.nextLine()”次数多于行数,您将遇到一个名为的异常。

      "java.util.NoSuchElementException: No line found".

如果您必须调用“sc.nextLine()” n 次,那么您必须至少 n 行。

尝试更改您的代码以将您调用“sc.nextLine()”的次数与行数相匹配,我保证您的问题将会得到解决。

Your real problem is that you are calling "sc.nextLine()" MORE TIMES than the number of lines.

For example, if you have only TEN input lines, then you can ONLY call "sc.nextLine()" TEN times.

Every time you call "sc.nextLine()", one input line will be consumed. If you call "sc.nextLine()" MORE TIMES than the number of lines, you will have an exception called

      "java.util.NoSuchElementException: No line found".

If you have to call "sc.nextLine()" n times, then you have to have at least n lines.

Try to change your code to match the number of times you call "sc.nextLine()" with the number of lines, and I guarantee that your problem will be solved.

逐鹿 2024-12-09 00:39:29

需要使用top comment但也要注意nextLine()。要消除此错误,只需

sc.nextLine()

从 while 循环内调用 Once

 while (sc.hasNextLine()) {sc.nextLine()...}

您正在使用 while 仅向前查看 1 行。然后使用 sc.nextLine() 读取您要求 while 循环向前查看的单行前面的 2 行。

还将多个 IF 语句更改为 IF, ELSE 以避免读取多行。

Need to use top comment but also pay attention to nextLine(). To eliminate this error only call

sc.nextLine()

Once from inside your while loop

 while (sc.hasNextLine()) {sc.nextLine()...}

You are using while to look ahead only 1 line. Then using sc.nextLine() to read 2 lines ahead of the single line you asked the while loop to look ahead.

Also change the multiple IF statements to IF, ELSE to avoid reading more than one line also.

靖瑶 2024-12-09 00:39:29

我遇到了这个问题,我的结构是:
1 - 系统
2 - 注册<-> 3 - 验证

我在这 3 个步骤中的每一个步骤中都关闭了扫描仪。我开始仅在系统中关闭扫描仪,问题就解决了。

I ran into this problem, my structure was:
1 - System
2 - Registration <-> 3 - validate

I was closing Scanner on each of the 3 steps. I started to close the Scanner only in system and it solved.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文