0
我有一個文件,我試圖收集數據,所以我可以將數據添加到對象創建,但它似乎並沒有工作,林不知道爲什麼。從文件讀取數據(java)
這是試圖讀取數據的代碼:
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但
我想你會發現hasNextDouble()對任何int都返回true。 – shmosel