我一直在研究这种ATM(最多可以有50个客户),在那里读取.txt文件,然后创建多个实例,将它们存储在一个数组中,以便其他类可以呼吁他们。 当我阅读文件时,只有最后一位客户的信息输入正确 - 我不能让前几位客户的输出正确。读取实例时出错
有每个“设置”多种方法,以防万一现场说“没有”,这样我就可以把它作为一个
Double.NaN or null, for example.
我已经在几个网站的研究,并有WASN尽管在一个网站上,它说我应该声明它像一个普通的数组。
private static String firstname = "";
private static String lastname = "";
private static int sin = 0;
private static int year = 0;
private static int month = 0;
private static int day = 0;
private static double sbalance = 0.0;
private static double cbalance = 0.0;
private static double cardbal = 0.0;
private static boolean confirm = false;
public int customernumber;
public static customer [] customerarray = new customer [50];
public static void readfile(){
String sb = "";
String cb = "";
String ca = "";
int counter = 0;
String thisLine;
try {
BufferedReader br = new BufferedReader(new FileReader("bankinfo.txt"));
while ((thisLine = br.readLine()) != null) {
customerarray[counter].setLastName(thisLine);
System.out.print (customerarray[counter].getLastName());
customerarray[counter].setFirstName(br.readLine());
System.out.print (customerarray[counter].getFirstName());
customerarray[counter].setSin(Integer.parseInt(br.readLine()));
System.out.print (customerarray[counter].getSin());
customerarray[counter].setYear(Integer.parseInt(br.readLine()));
System.out.print (customerarray[counter].getYear());
customerarray[counter].setMonth(Integer.parseInt(br.readLine()));
System.out.print (customerarray[counter].getMonth());
customerarray[counter].setDay(Integer.parseInt(br.readLine()));
System.out.print (customerarray[counter].getDay());
sb = br.readLine();
if (sb.equals("none")){
customerarray[counter].setSBalance("none") ;
System.out.print (customerarray[counter].getSBalance());
}
else {
customerarray[counter].setSBalance(Double.parseDouble(sb));
System.out.print (customerarray[counter].getSBalance());
}
cb = br.readLine();
if (cb.equals ("none")){
customerarray[counter].setCBalance ("none");
}
else if (cb != "none"){
customerarray[counter].setCBalance(Double.parseDouble(cb));
}
else{
System.out.print ("error CBalance");
}
ca = br.readLine();
if (ca.equals("none")){
customerarray[counter].setSBalance("none") ;
}
else {
customerarray[counter].setCardbal(Double.parseDouble(ca));
}
counter = counter + 1;
}
br.close();
}
catch (IOException e) {
System.err.println("Error: " + e);
}
}
该文本文件相当简单 - 它由每个客户的9个字段组成。 如果他们没有一个特定的帐户,它被列为'none',并且当阅读器读取它们时,它使用带有String输入的变体方法,并设置double = Double.NaN();
以下是文本文件的示例。 每个客户有9个字段。
Tam
Christian
984635684
1996
6
12
none
10233.52
none
Yang
Wesley
324917400
1996
8
1
3233.36
none
none
Lin
Sophia
1984
1985
5
6
912.12
58.96
95.63
什么是Double.DaD?你的意思是['Double.NaN'](http://docs.oracle.com/javase/7/docs/api/java/lang/Double.html#NaN)? – Makoto
哎呀,我的不好,我的主程序中有错,它只是滑了我的脑海。 – kryyn
你发布的代码不会运行,没有客户对象的构造函数调用 –