【发布时间】:2017-07-20 00:14:21
【问题描述】:
比如我想把这个文件的逗号分开,读取每一行的第一个字符。基于第一个字符(#,*,@),我想使用每个字符之后的数据创建一个对象(Steve Davis 2000)。
4
#Steve,Davis,2000
*John,Kanet,800,7000,0.10
@Thomas,Hill,20,50
*Lisa,Green,800,6000,0.10
4 用作我的数组的大小
到目前为止,这是我的代码:
import java.util.Scanner;
import java.io.*;
public class PayRoll3{
public static void main(String[] args) throws FileNotFoundException{
Scanner input=new Scanner(new File(args[0]));
int size=input.nextInt();
input.nextLine();
Employee[] employees = new Employee[size];
int index = 0;
while(input.hasNext()){
String tmp= input.next();
String[] commas = tmp.split(",");
if(tmp.substring(0,1).equals("#")){
employees[index++]=new Manager2(input.next(), input.next(), input.nextDouble() );
}
else if(tmp.substring(0,1).equals("*")){
employees[index++]=new CommissionEmployee2(input.next(), input.next(), input.nextDouble(), input.nextDouble(), input.nextDouble());
}
else if(tmp.substring(0,1).equals("@")){
employees[index++]=new HourlyWorker2(input.next(), input.next(), input.nextDouble(), input.nextDouble());
}
}
input.close();
for ( Employee currentEmployee : employees ){
System.out.println( currentEmployee );
}
}
当我运行我得到的代码时
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at PayRoll3.main(PayRoll3.java:19)
【问题讨论】:
-
欢迎来到本站!这是一个很好的问题的一个很好的开始——你有一个问题的描述,你也展示了你试图解决它的代码。缺少的一件事是对运行代码时发生的情况的描述。点击edit,并在您的问题底部添加此描述。
-
能否粘贴堆栈跟踪以便我们定位错误?
标签: java arrays file polymorphism