获取异常java.util.NoSuchElementException:从文件读取时未找到任何行?

获取异常java.util.NoSuchElementException:从文件读取时未找到任何行?,java,debugging,Java,Debugging,该代码以前工作正常,但即使未进行任何更改,也会出现运行时异常。这些程序的目的是,成为一个棒球运动员的数据库,其中包含了他们的一些统计数据——可以在下面的代码中看到。当我现在运行程序时,我得到了这个错误 Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Scanner.java:1540) at Assig3.getBatt

该代码以前工作正常,但即使未进行任何更改,也会出现运行时异常。这些程序的目的是,成为一个棒球运动员的数据库,其中包含了他们的一些统计数据——可以在下面的代码中看到。当我现在运行程序时,我得到了这个错误

Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Scanner.java:1540)
    at Assig3.getBatters(Assig3.java:47)
    at Assig3.<init>(Assig3.java:62)
    at Assig3.main(Assig3.java:248)
batters.txt(文本文件):

原因是当您的代码到达最后一个文件条目时,即
30

hr = inScan.nextInt();  inScan.nextLine();
     //^^^ this will read 30
                  // but         ^^^ there is no next line and hence the exception 
解决方案:使用
hasNextLine
或在文件末尾使用enter键添加nextline

hr = inScan.nextInt();  
if(inScan.hasNextLine())
      inScan.nextLine();

或者,您可以操作文件,然后只需在文件末尾使用enter(我不建议使用enter)添加一个空行即可。

您的程序未编译,或者在执行时出错?作为命令行参数传递的文件路径是有效路径?表示文件实际存在于该路径。从异常情况看,您的文件是空的,或者位于第47行,因此如果转到第47行,您将发现file/path/empty file没有问题
public class Batter {

    private String firstName;
    private String lastName;
    private int atBats;
    private int hits;
    private int doubles;
    private int triples;
    private int homeRuns;

    public Batter(String fName, String lName){
        firstName = fName;
        lastName = lName;
    }
    public void setBats(int batCount){
        atBats = batCount;
    }
    public void setHits(int hitCount){
        hits = hitCount;
    }
    public void setDoubles(int doubleCount){
        doubles = doubleCount;
    }
    public void setTriples(int tripleCount){
        triples = tripleCount;
    }
    public void setHR(int homeRunCount){
        homeRuns = homeRunCount;
    }
    public double getAve(){
        double one = this.hits;
        double two = this.atBats;
        double average = (one / two);
        return average;
    }
    public int getAtBats(){
        return this.atBats;
    }
    public int getHits(){
        return this.hits;
    }
    public int getDoubles(){
        return this.doubles;
    }
    public int getTriples(){
        return this.triples;
    }
    public int getHomeRuns(){
        return this.homeRuns;
    }
    public String getName(){
        String fullName = this.lastName + "," + this.firstName;
        return fullName;
    }
    public boolean equals(){

        return true;
    }
    public String toString(){
        return "Player: "+getName()+"\nAt Bats: "+this.atBats+"\nHits: "+this.hits+"\nDoubles: "+this.doubles+"\nTriples: "+this.triples+"\nHome Runs: "+this.homeRuns;
    }
}
5
Cannot
Hit
635
155
12
7
6
Major
Hitter
610
290
50
25
65
The
Hulk
650
300
0
0
300
Iron
Man
700
600
300
0
300
Herb
Weaselman
600
200
20
15
30
Error :
Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Scanner.java:1540)
    at Assig3.getBatters(Assig3.java:47)
    at Assig3.<init>(Assig3.java:62)
    at Assig3.main(Assig3.java:248)
NoSuchElementException mean whatever your code is trying to read is   not there 
mean there is no element which you are trying to read.
At line 47 i.e hr = inScan.nextInt();  inScan.nextLine(); 
your code is trying to read an int and nextline 
but there is no next line in your source file 
which basically will happen in the last iteration of your loop
hr = inScan.nextInt();  inScan.nextLine();
     //^^^ this will read 30
                  // but         ^^^ there is no next line and hence the exception 
hr = inScan.nextInt();  
if(inScan.hasNextLine())
      inScan.nextLine();