我无法让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)
主类:
/**
* 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
员工类:
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;
}
}
以下是我要阅读的文字:
Doe John 13.00 44.5
Doe John 13.00 44.5
Doe John 13.00 44.5
Doe John 13.00 44.5
答案 0 :(得分:2)
您需要在循环底部调用Scanner.nextLine()
,即本例sInputFile.nextLine()
。你没有前进到下一行,所以你在这一行上用尽了令牌。
答案 1 :(得分:-2)
这将有效..
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);