Java 扫描程序:NoSuchElementException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28532063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:46:04  来源:igfitidea点击:

Java Scanner: NoSuchElementException

javajava.util.scanner

提问by rc dude

I am having trouble getting my Scannerto work. It is based on an example my teacher gave us to modify, but I keep getting errors when I run it. When I run it I get this error:

我在Scanner上班时遇到了麻烦。它基于我老师给我们修改的一个示例,但我在运行时不断出错。当我运行它时,我收到此错误:

java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1371)
at ReadDataFile.main(ReadDataFile.java:25)

Main Class:

主要类:

  /**
 * Write a description of class ReadDataFile here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.ArrayList;
public class ReadDataFile {
    public static void main(String[] args) throws IOException {

        Scanner sInputFile=null;
        String FirstName,LastName;
        Float PayRate, HoursWorked;

        ArrayList<Employee> Records = new ArrayList<Employee>();

        try {
            sInputFile = new Scanner(new BufferedReader(new FileReader("c:\temp\inputData.txt")));
            while (sInputFile.hasNextLine()) {
               FirstName = new String(sInputFile.next());
               if(FirstName.length() > 9)
               FirstName = FirstName.substring(0,8);
               LastName = new String(sInputFile.next());
               if(LastName.length() > 9)
               LastName = LastName.substring(0,8);
               PayRate = sInputFile.nextFloat();
               HoursWorked = sInputFile.nextFloat();
               Employee worker = new Employee(LastName,FirstName,PayRate,HoursWorked);
               Records.add(worker);
            } // while   

        } // try
        catch(Exception IOException)
        {
           System.out.println("Unknown file error occurred ...");
        }
        finally { sInputFile.close();}

        System.out.print("Complete");
    } // main
} // class

Employee Class:

员工等级:

public class Employee
{
  String LastName, FirstName;
  float PayRate, HoursWorked, Net, Gross, Tax, TaxRate;

  public Employee(String LN, String FN, float Rate, float Hours) 
  {
    LastName = LN;
    FirstName = FN;
    PayRate = Rate;
    HoursWorked = Hours;
    Gross = PayRate * HoursWorked;
    Net = Gross * (1-TaxRate);
    Tax = Gross * TaxRate;

  }

  public float getPayRate()
  {
      return PayRate;
    }
    public float getHoursWorked()
  {
      return HoursWorked;
    }
    public float getNet()
  {
      return Net;
    }
    public float getGross()
  {
      return Gross;
    }
    public String getLastName()
  {
      return LastName;
    }
    public String getFirstName()
  {
      return FirstName;
    }
  public float getTax()
  {
      return Tax;
    }
}

Here is the text I am trying to read:

这是我正在尝试阅读的文本:

 Doe   John  13.00  44.5
 Doe   John  13.00  44.5
 Doe   John  13.00  44.5
 Doe   John  13.00  44.5

回答by user207421

You need to call Scanner.nextLine()at the bottom of the loop, i.e. in this case sInputFile.nextLine(). You aren't advancing to the next line, so you run out of tokens on this line.

您需要Scanner.nextLine()在循环的底部调用,即在这种情况下sInputFile.nextLine()。你没有前进到下一行,所以你用完了这一行的令牌。

回答by crAlexander

This will work..

这将工作..

try {
       //input File
        sInputFile = new Scanner(new File("C:/Users/Alex.hp/Desktop/t.txt"));

        while (sInputFile.hasNextLine()) { //your code
           FirstName = new String(sInputFile.next());
           if(FirstName.length() > 9)
           FirstName = FirstName.substring(0,8);
           LastName = new String(sInputFile.next());
           if(LastName.length() > 9)
           LastName = LastName.substring(0,8);
           PayRate =Float.parseFloat(sInputFile.next()); //edited cause of Exception(InputMismatch Error)
           HoursWorked =Float.parseFloat(sInputFile.next()); //edited cause of Exception(InputMismatch Error)
           Employee worker = new Employee(LastName,FirstName,PayRate,HoursWorked);
           Records.add(worker);
        } // while   

    }catch(Exception ex){
        ex.printStackTrace();
    }

//See the results
 for(int i=0; i<Records.size(); i++)
        System.out.println(Records.get(i).FirstName+"    "+Records.get(i).LastName +" " +Records.get(i).PayRate +" "+Records.get(i).HoursWorked);

enter image description here

在此处输入图片说明