从 java 读取文件?
reading files from java?
我有一项任务,我必须制作一个简单的银行应用程序。我需要将客户信息存储在一个文件中,其中包含名字和姓氏、客户 ID 和余额。然后我需要创建一个菜单选项来检查余额、取款、存款和退出。我目前遇到的问题是当用户想要查看余额时试图显示余额。余额在我的文件的第四行,我不确定如何只显示第四行然后能够使用该数字来添加或减去一个数字。我正在尝试在 switch 语句中的 case 1 中执行此操作。任何提示将不胜感激。
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();
}
}
有很多方法可以做到这一点。一种方法是从 Scanner
对象中读取所有行并将其转换为列表 (ArrayList) 或字符串数组。即Array或List中的每一项对应文件的一行。
列表或数组的索引将提供该特定文件的内容。
============================================= ================================
好的,根据您的评论,既然您还没有学习数组,您可以按照阅读银行客户名称的方式来学习
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);
}
当然,答案只是指示性的,不会进行所有空值检查,并假定文件格式与您所说的完全一致。
最简单的方法是简单地阅读并忽略前三行,方法是:
for (int i = 0; i < 3; i++)
inputFile.nextLine();
然后用Integer.parseInt()方法读取第四行作为整数。
int total = Integer.parseInt (inputFile.nextLine ());
请注意,在这两个片段中都省略了错误和异常处理。
如果不重新创建您的数据和代码,您似乎正在将整行数据读取给 bankcustomer。如果您的数据文件结构是逗号分隔的,那么您可以将该行拆分为多个部分。定义一个数据class
public static class customer {
public String first;
public String last;
public String customer_i;
public double balance:
}
将行转换为数据后 class,您可以通过以下方式引用每个数据:
balance = customer.balance. etc.
这是近似值,希望对您有所帮助。
您可以读取 3 行而不保存它们的值,
或在每一行中搜索关键字:
int balance;
if(!bankCustomer.indexOf("balance") != -1)
{
balance = Integer.parseInt(bankCustomer);
}
我有一项任务,我必须制作一个简单的银行应用程序。我需要将客户信息存储在一个文件中,其中包含名字和姓氏、客户 ID 和余额。然后我需要创建一个菜单选项来检查余额、取款、存款和退出。我目前遇到的问题是当用户想要查看余额时试图显示余额。余额在我的文件的第四行,我不确定如何只显示第四行然后能够使用该数字来添加或减去一个数字。我正在尝试在 switch 语句中的 case 1 中执行此操作。任何提示将不胜感激。
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();
}
}
有很多方法可以做到这一点。一种方法是从 Scanner
对象中读取所有行并将其转换为列表 (ArrayList) 或字符串数组。即Array或List中的每一项对应文件的一行。
列表或数组的索引将提供该特定文件的内容。
============================================= ================================
好的,根据您的评论,既然您还没有学习数组,您可以按照阅读银行客户名称的方式来学习
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);
}
当然,答案只是指示性的,不会进行所有空值检查,并假定文件格式与您所说的完全一致。
最简单的方法是简单地阅读并忽略前三行,方法是:
for (int i = 0; i < 3; i++)
inputFile.nextLine();
然后用Integer.parseInt()方法读取第四行作为整数。
int total = Integer.parseInt (inputFile.nextLine ());
请注意,在这两个片段中都省略了错误和异常处理。
如果不重新创建您的数据和代码,您似乎正在将整行数据读取给 bankcustomer。如果您的数据文件结构是逗号分隔的,那么您可以将该行拆分为多个部分。定义一个数据class
public static class customer {
public String first;
public String last;
public String customer_i;
public double balance:
}
将行转换为数据后 class,您可以通过以下方式引用每个数据:
balance = customer.balance. etc.
这是近似值,希望对您有所帮助。
您可以读取 3 行而不保存它们的值, 或在每一行中搜索关键字:
int balance;
if(!bankCustomer.indexOf("balance") != -1)
{
balance = Integer.parseInt(bankCustomer);
}