0

我一直在研究这种 ATM(最多有 50 个客户),我在其中读取一个 .txt 文件,然后创建多个实例,将它们存储在一个数组中,以便其他类可以调用它们。当我阅读文件时,只有最后一个客户的信息输入正确——我永远无法让前几个客户的输出正确。

每个“Set”都有多种方法,以防万一该字段显示“none”,以便我可以将其保留为

   Double.NaN or null, for example.

我在几个网站上进行了研究,关于将实例存储在数组中的内容并不多,尽管在一个网站上,它说我应该像普通数组一样声明它。

  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
4

1 回答 1

0

我看不到您在哪里customer[]用实际customer对象实例化每个单独的位置。

while在循环开始之后添加此行:

customerarray[counter] = new customer();

创建对象数组时,其中的所有元素默认为空。你不能取消引用 null,所以你遇到了问题。

于 2012-06-17T01:54:10.143 回答