2012-06-17 5 views
0

.txt 파일을 읽은이 ATM (최대 50 명의 고객)에서 작업하고 여러 인스턴스를 만들어 배열에 저장하여 다른 클래스에서 그들에게 전화하십시오. 파일을 읽었을 때 마지막 고객의 정보 만 올바르게 입력되었습니다. 처음 몇 고객의 출력 결과가 정확하지 않습니다.인스턴스를 읽을 때 오류가 발생했습니다.

내가 몇 웹 사이트에 연구를 가지고

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 개의 필드로 구성되어 있습니다. 특정 계정이 없으면 '없음'으로 표시되고 판독기가이를 읽으면 String 입력이있는 변형 메서드를 사용하고 double = Double.NaN();

다음은 텍스트 파일의 예입니다. 고객마다 9 개의 필드가 있습니다. 당신이 실제 customer 객체로 customer[]의 각각의 위치를 ​​인스턴스화하고 어디는 표시되지 않습니다

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 
+0

다시 'Double.DaD'란 무엇입니까? ['Double.NaN']을 의미합니까 (http://docs.oracle.com/javase/7/docs/api/java/lang/Double.html#NaN)? – Makoto

+0

와플, 내 잘못, 내 주 프로그램에서 맞았 어, 그냥 내 마음을 미끄러. – kryyn

+1

게시 한 코드가 실행되지 않습니다. 고객 개체의 생성자 호출이 없습니다. –

답변

0

.

while 루프의 시작 후 전에이 줄을 추가

customerarray[counter] = new customer(); 

객체 배열을 생성, 그것의 모든 요소가 디폴트로 null. null을 역 참조 할 수 없으므로 문제가 발생하고 있습니다.

+0

이 코드는 잘못된 것입니까? public static customer [] 고객 배열 = 신규 고객 [50]; – kryyn

+0

배열의 * 요소에는 * 만 공백을 사용할 수 있지만 배열의 내부 요소에는 사용할 수 없습니다. – Makoto

관련 문제