2016-09-16 43 views
1
import java.util.*; 
import java.io.*; 
public class ConcreteClass 
{ 
    private String fileName = "List.txt"; 
    private Customer[] clients = null; 

    public static void main(String[] args) throws Exception 
    { 

     ConcreteClass first = new ConcreteClass(); 


     first.readFile(); 
     first.showCustomers(); 


     System.out.println("Hello World"); 

    } 

    public void showIntro() 
    { 
     System.out.println("---------BANKING MANAGEMENT PROGRAM---------"); 
     System.out.println("1---Show list of customers."); 
     System.out.println("2---Add a Customer bank account "); 
     System.out.println("3---Remove a Customer bank account "); 
     System.out.println("4---Sort customer list according to name"); 
     System.out.println("5---Sort customer list according to account balance"); 
    } 

    public void readFile() throws Exception 
    { 
     int index = 0; 
     Scanner reader = new Scanner(new File(fileName)); 

     int count=0; 

     while(reader.hasNextLine()) 
     { 
      count++; 
     } 


     while(reader.hasNextLine()) 
     { 
      String[] roster = reader.nextLine().split(","); 

      String fName = roster[0]; 
      String lName = roster[1]; 
      String mName = roster[2]; 
      double balance = Double.parseDouble(roster[3]); 
      String accNo = roster[4]; 

      Account b = new Account(balance,accNo); 
      Customer c = new Customer(fName,lName,mName,b); 

      clients = new Customer[count]; 
      clients[index++] = c; 
     } 

     reader.close(); 

    } 

    public void showCustomers() 
    { 
     for(int i =0; i<clients.length; i++) 
     { 
      System.out.println(clients[i].toString()); 
     } 
    } 



} 

Account(balance,account number)-toString(): ("Account Number: "+getNumber()+"/n Account Balance: "+getBalance()) 

Customer(fName,lName,mName,account)-toString(): ("Customer Name:"+getFirstName()+" "+getMidName()+" "+getLastName()+" Customer Account:"+getAccount()) 

回答

7

在您的:

while(reader.hasNextLine()) 
{ 
    count++; 
} 

你只是檢查讀者是否有下一行,但你不會更進一步。我的意思是想象你在第一線,並檢查是否有第二條線。然後在下一次迭代中,你仍然在第一行。您需要更進一步,像這樣:

reader.nextLine()

此外,如果你這樣做,你的第一個while那麼你的第二while將無法​​運行,你需要採取的一些其他方式的照顧。另一件事 - 你對這個數組(客戶端)做的事情沒有意義,你需要在這些循環之前創建數組。

2

這將導致你的代碼永遠運行:

 while(reader.hasNextLine()) 
    { 
     count++; 
    } 

你必須調用裏面reader.nextLine(),否則無限循環

0

我不是專家,但首先執行的是主要方法。

ConcreteClass first = new ConcreteClass(); 
first.readFile(); 
first.showCustomers(); 
System.out.println("Hello World"); 

此方法後來調用其他類和方法。

至少你應該打印Hello World。 (除了循環使程序永遠執行......防止這種情況發生)問題也可能是結構。你不需要這麼長的程序來打印你好世界。打印的主要方法應該足夠了。

確保您瞭解方法的連接。

1
while(reader.hasNextLine()) 
    { 
     count++; 
    } 

你永遠不會擺脫這個while循環。作爲未來的建議,如果您使用的是Eclipse環境,那麼您可以通過在代碼的任意一行上設置斷點(ctrl + shift + b)來進入「調試模式」,然後在該行程序停止後你可以使用f5-7來瀏覽你的代碼。

當我擔心我的程序陷入某種循環的某處時,我覺得這很有幫助。