我有一个文件,我试图收集数据,所以我可以将数据添加到我创建的对象,但它似乎没有工作,我不知道为什么。
这是尝试读取数据的代码:
private void loadEmployeesFromFile(String fileName)
{
File file = new File(fileName);
Scanner scan = null;
try {
scan = new Scanner(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int counter = 0;
while(scan.hasNext())
{
scan.nextLine();
counter++;
}
scan.close();
employees = new Employee[counter];
Scanner fileScan = null;
try
{
fileScan = new Scanner(file).useDelimiter(",");
}
catch(FileNotFoundException e)
{
System.out.print("file was not found :/");
}
for(int i = 0; i < 3; i++ )
{
String id = fileScan.next();
String firstName = fileScan.next();
String lastName = fileScan.next();
String hireDate = fileScan.next();
int shift = fileScan.nextInt();
double hourlyPayRate = 0;
double bonusRate = 0;
switch(id.substring(0, 2))
{
case "PW":
if(fileScan.hasNextDouble())
hourlyPayRate = fileScan.nextDouble();
else if(fileScan.hasNextInt())
hourlyPayRate = (double)fileScan.nextInt();
employees[i] = new ProductionWorker(id, firstName, lastName, hireDate, shift, hourlyPayRate);
break;
case "TM":
if(fileScan.hasNextDouble())
hourlyPayRate = fileScan.nextDouble();
else if(fileScan.hasNextInt())
hourlyPayRate = (double)fileScan.nextInt();
if(fileScan.hasNextDouble())
bonusRate = fileScan.nextDouble();
else if(fileScan.hasNextInt())
bonusRate = (double)fileScan.nextInt();
double reqTrainingHrs = fileScan.nextDouble();
double trainingHrsDone = fileScan.nextDouble();
employees[i] = new TeamLeader(id, firstName, lastName, hireDate, shift, hourlyPayRate, bonusRate, reqTrainingHrs, trainingHrsDone);
break;
case "SU":
double annualSalary = fileScan.nextDouble();
bonusRate = fileScan.nextDouble();
employees[i] = new ShiftSupervisor(id, firstName, lastName, hireDate, shift, annualSalary, bonusRate);
break;
}
}
fileScan.close();
}
这是数据文件:
PW_1234,James,Bond,01/02/10,1,10
PW_1235,John,Brown,02/03/10,2,10.5
PW_1236,Howard,Johnson,03/04/10,3,11
PW_1237,Francis,Themule,04/05/11,4,10.75
PW_1238,Mathew,Lewis,05/06/11,1,12.75
PW_1239,Mark,Bixton,05/13/11,2,13
PW_1242,Sarah,Glover,05/14/11,1,13.75
PW_1245,John,Doe,05/15/11,4,10.5
PW_1245,Mary,Doe,05/15/11,4,10.5
TL_1248,Abel,English,05/16/11,3,16.5,0.01,100,89
TL_1251,Eustis,Clauser,05/17/11,2,16,0.02,100,9
SU_1254,Henry,Hollowman,05/18/11,1,40000,0.01
PW_1240,Luke,Sailor,01/22/12,3,14.5
PW_1243,Jane,Baker,01/23/12,2,14
PW_1243,Jane,Baker,01/23/12,2,14
TL_1246,David,Brief,01/24/12,1,14.75,0.01,100,57
PW_1246,David,Doson,01/24/12,1,14.75
TL_1249,Baker,Anderson,01/25/12,4,11.5,0.01,100,100
TL_1252,Frank,Donson,01/26/12,3,17.5,0.02,100,39
SU_1255,Issac,Asimov,01/27/12,2,43000,0.02
SU_1256,Issac,Shoreman,01/28/12,3,39000,0.01
SU_1257,Issac,Roberts,01/29/12,4,35500,0.01
PW_1241,John,Candy,11/23/13,4,9.5
PW_1244,Kate,Smith,11/24/13,3,15.5
PW_1244,Kate,Handle,11/24/13,3,15.5
TL_1247,Samual,Dempky,11/25/13,2,15,0.01,100,10
TL_1250,Charley,Boman,11/26/13,1,15.75,0.01,100,50
TL_1253,George,Fritzmen,11/27/13,4,12.5,0.02,100,27
由于某些奇怪的原因,当它在读取名称后到达TL_1251时会立即跳过尝试读取班次。
这是生成的错误:
Exception in thread "AWT-EventQueue-0" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at assignment3.FactoryEmployees.loadEmployeesFromFile(FactoryEmployees.java:244)
at assignment3.FactoryEmployees.<init>(FactoryEmployees.java:43)
at assignment3.FactoryEmployees$1.run(FactoryEmployees.java:403)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
第244行是
int shift = fileScan.nextInt();
当限制循环仅运行三次并且仅打印班次时,我得到
PW_1234
James
Bond
01/02/10
1
10
PW_1235
John
Brown
02/03/10
2
10.5
PW_1236
Howard
Johnson
03/04/10
3
奇怪的是它最终会打印出我尚未编码打印的hourlyPayRate
答案 0 :(得分:0)
看起来你有一个错字。尝试将case "TM"
更改为case "TL"
。