我试图要求用户输入" doDeposit"方法并检查输入的ID是否与文件中的ID匹配,但是我收到错误"在java.util.Scanner.throwFor(未知来源)"尝试使用扫描仪时。
我程序读取的文件包含以下数据:
Brain Adams,51,B10000,3000.5,1
John Waterson,30,B10001,4000.34,0
John Key,61,B10002,56666600,2
Julia Roberts,28,B10003,454545,1,
Tom Cruise,27,B10004,340000000,1
Johnny Tom,54,B10005,230,0
Joe Haldeman,66,B10006,23055.5,0
Charles King,55,B10007,2400.5,0
James Thomson,44,B10008,330.5,0
Cameron Diaz,28,B10009,23000000.6,1
Barak Obama,84,B10010,2000000.4,2
Dustin Hoffman,37,B10011,23000000,1
Robin Williams,72,B10012,22000000.5,1
Russell Crowe,47,B10013,44000000.3,1
Kylie Minogue,28,B10014,10000000.3,1
Paul Keating,41,B10015,4000000.8,2
Lady Gaga,32,B10016,50000000,1
Helen Clark,502,B10017,400000,2
Mikhail Suzenski,32,B10018,0.0,0
Boris Yeltsin,21,B10019,10000000.5,2
我的代码:
package assignment;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import static java.lang.System.out;
public class Assignment
{
public static void main(String[] args)
{
try
{
new
BankAccountApp("C:/Users/USER/workspace/assignment/data/AssignmentData");
}
catch (IOException ioe)
{
out.printf("Missing file: %sC:/Users/USER/workspace/assignment/data/AssignmentData? \n\n",
new Assignment().getClass().getPackage().getName());
}
}
}
class BankAccountApp
{
private List<BankAccount> accounts;
public BankAccountApp(String filename) throws IOException
{
readAccountData(filename);
displayMenu();
int opt = 0;
try (Scanner input = new Scanner(System.in))
{
while (true)
{
out.print("\nSelect an option: ");
opt = input.nextInt();// format exception if string
if (opt < 0 || opt > 7)
out.println("Invalid. Must be 1 - 7 (or 0 for menu).");
else
break;
}
}
switch (opt)
{
case 0: displayMenu(); break;
case 1: doDeposit(); break;
case 2: doWithdraw(); break;
case 3: displayHighestBalanceAccount(); break;
case 4: displayMostActiveAccount(); break;
case 5: displayAllAccounts("C:/Users/USER/workspace/assignment/data/AssignmentData"); break;
case 6:
{
int total = getTotalTransactions(accounts);
out.println("Total transactions: " + total);
break;
}
case 7: farewell(); return;
}
}
public void readAccountData(String fn) throws IOException
{
accounts = new LinkedList<>();
Path path = new File("C:/Users/USER/workspace/assignment/data/AssignmentData").toPath();
List<String> content = Files.readAllLines(path);
for(String line : content){
String[] items = line.split(",");
String name = items[0];
int age = Integer.valueOf(items[1]);
String accountID = items[2];
double balance = Double.valueOf(items[3]);
int accountType = Integer.valueOf(items[4]);
BankAccount b = new BankAccount(name, age, accountID, balance, accountType);
accounts.add(b);
}
}
public void displayAllAccounts(String fn) throws IOException
{
accounts = new LinkedList<BankAccount>();
Path path = new File(fn).toPath();
List<String> content = Files.readAllLines(path);
for(String line : content){
String[] items = line.split(",");
String name = items[0];
int age = Integer.valueOf(items[1]);
String accountID = items[2];
double balance = Double.valueOf(items[3]);
int accountType = Integer.valueOf(items[4]);
BankAccount b = new BankAccount(name, age, accountID, balance, accountType);
accounts.add(b);
out.printf("---All Bank Accounts---\n");
out.println("Name: "+name);
out.println("Age: "+age);
out.println("Account ID: "+accountID);
out.printf("Account Balance: %f",balance);
out.println("\nAccount Type: "+accountType+"\n");
}
return;
}
public void displayHighestBalanceAccount()
{
BankAccount mostMoney = accounts.get(3);
for(BankAccount b : accounts){
if(mostMoney.getbalance() < b.getbalance()){
mostMoney = b;
}
}
String name = mostMoney.getname();
int age = mostMoney.getage();
String accountID = mostMoney.getaccountID();
double balance = mostMoney.getbalance();
int accountType = mostMoney.accountType();
out.printf("---Bank account with the highest balance---\n");
out.println("Name: "+name);
out.println("Age: "+age);
out.println("Account ID: "+accountID);
out.printf("Account Balance: %f",balance);
out.println("\nAccount Type: "+accountType);
}
public void displayMostActiveAccount()
{
}
public int getTotalTransactions(List<BankAccount> list)
{
return 0;
}
public void doDeposit()
{
out.print("Enter an account ID for deposit: ");
}
public void doWithdraw()
{
}
public void farewell()
{
out.println("Thanks for using the service. Bye!");
}
public void displayMenu()
{
out.println("*********************************");
out.println("* Bank Account Operation Menu *");
out.println("***********************************\n");
out.println("0. Display Menu");
out.println("1. Deposit");
out.println("2. Withdraw");
out.println("3. Display Highest Balance Account");
out.println("4. Display Most Active Account");
out.println("5. Display All Accounts");
out.println("6. Display Total Number of Transactions");
out.println("7. Exit");
}
}
class BankAccount1 extends Person1
{
private String accountID;
private double balance;
private int accountType;
public String getaccountID() {
return accountID;
}
public void setaccountID(String accountID) {
this.accountID = accountID;
}
public double getbalance() {
return balance;
}
public void setbalance(double balance) {
this.balance = balance;
}
public int accountType() {
return accountType;
}
public void setaccountType(int accountType) {
this.accountType = accountType;
}
public BankAccount1(String name, int age, String accountID, double balance, int accountType)
{
super(name, age);
this.accountID = accountID;
this.balance = balance;
this.accountType = accountType;
}
}
class Person1 {
private String name;
private int age;
public String getname() {
return name;
}
public void setname(String name) {
this.name = name;
}
public int getage() {
return age;
}
public void setage(int age) {
this.age = age;
}
public Person1(String name, int age)
{
this.name = name;
this.age = age;
}
}
非常感谢任何指针。
答案 0 :(得分:0)
我运行了你的代码。我发现了两个错误。
您使用错误的路径 看你的路径是
C:/Users/USER/workspace/assignment/data/AssignmentData
但是Path应该如下所示
C:\\Users\\Menuka\\IdeaProjects\\JavaTest\\src\\AssignmentData`
我将在下面发布完整代码。
https://gist.github.com/Menuka5/2af4d16dccbdf780d730fd8bf5ce4e56