2015-01-15 129 views
-1

我有一個任務,我必須做一個簡單的銀行應用程序。我需要將客戶信息存儲在名字,客戶ID和餘額的文件中。然後我需要創建一個菜單選項來檢查餘額,退出,存款和退出。我目前遇到的問題是在用戶想要檢查餘額時試圖顯示餘額。餘額是在我的文件的第四行,我不知道如何只顯示第四行,然後能夠使用該數字來添加或減去一個數字。我正試圖在switch語句中的case 1中執行此操作。任何提示將不勝感激。從java讀取文件?

import java.util.Scanner; 
import java.text.DecimalFormat; 
import java.io.*; 
public class bank { 

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


     String fileName; // stores the name of the file 
     String bankCustomer; // used to display the customers bank information 
     int menuOption = 0; // allows the user to enter a number so they can select what they want to do 

     Scanner keyboard = new Scanner(System. in); 

     // enter the file name to see customer information 
     System.out.print("enter file name "); 
     fileName = keyboard.nextLine(); 

     File file = new File(fileName); 
     Scanner inputFile = new Scanner(file); 

     // read customer information from file i need this 
     while (inputFile.hasNext()) { 
      bankCustomer = inputFile.nextLine(); 
      System.out.println(bankCustomer); 
     } 

     System.out.println("Please enter the number for the following:\n 1) Balance \n 2) Deposit + \n 3) Withdrawal \n 4) Quit "); 

     menuOption = keyboard.nextInt(); 

     switch (menuOption) { 
      case 1: 
       // need to show balance how do i do that? 
       while (inputFile.hasNext()) { 

        bankCustomer = inputFile.nextLine(); 
        System.out.println(bankCustomer); 
       } 
       break; 
      default: 
       System.out.println("Invalid choice"); 
     } 
     inputFile.close(); 
    } 
} 

回答

0

有很多方法可以做到這一點。一種方法是讀取Scanner對象中的所有行,並將其轉換爲List(ArrayList)或String Array。這是陣列或列表中的每個項目都對應於文件的一行。

然後列表或數組的索引將提供該特定文件的內容。

============================================== ===============================

好吧根據你的評論,現在你還沒有學習陣列你能做到這一點你讀你的銀行客戶名稱

String bankCustomer = inputFile.nextLine(); 

//Assuming second line contains account number 
String bankAccountNumber = inputFile.nextLine(); 

// Assuming third line contains account type. 
String bankAccountType = inputFile.nextLine(); 

// Fourth line has your account balance 
String bankAccountBalanceStr = inputFile.nextLine(); 

double bankAccountBalance = 0d; 
if(null != bankAccountBalanceStr && 0 > bankAccountBalanceStr.length()){ 
    bankAccountBalance = Double.parseDouble(bankAccountBalanceStr); 
} 

的方式答案當然只是指示性的,並沒有完成所有的null檢查,並假設該文件格式是完全一樣的,你告訴。

+0

我們還沒有覆蓋陣列但在課堂上,所以我在尋找一種方式來做到這一點不array –

+0

@RishiDholliwar你有沒有講過創建類?如果是這樣,請創建包含所有這些字段的'User'類並填充它。 –

+0

@RishiDholliwar檢查出更新的答案 – owaism

0

最簡單的方法是簡單地通過執行類似讀取並忽略所述第一三線,:

for (int i = 0; i < 3; i++) 
    inputFile.nextLine(); 

,然後讀取第四線與所述的Integer.parseInt()方法的整數。

int total = Integer.parseInt (inputFile.nextLine()); 

請注意,在這兩個代碼段中,錯誤和異常處理均被省略。

+0

我粘貼在我的switch語句中,然後使用System.out.println(bankCustomer);沒有任何反應。 –

0

沒有重新創建您的數據和代碼,看起來您正在向bankcustomer讀取整行數據。如果您的數據文件結構是逗號分隔的,則可以將該行分割爲組件。

balance = customer.balance. etc. 

這是近似的,但我希望它能幫助:定義一個數據類

public static class customer { 
    public String first; 
    public String last; 
    public String customer_i; 
    public double balance: 

}

一旦你有翻譯成數據類行了,你可以通過引用每個。

+0

我仍然不知道如何在我的代碼中實現這個。我如何得到第一條線放在第一條或最後一條線以保持平衡。 –

0

您可以讀3行,而不是保存它們的值, 或搜索每行一個關鍵字:

int balance; 
if(!bankCustomer.indexOf("balance") != -1) 
{ 
    balance = Integer.parseInt(bankCustomer); 
}