2015-01-15 131 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); 
}