有关使用Scanner时InputMismatchException的问题 [英] Question about InputMismatchException while using Scanner
问题描述
问题:
输入文件:客户的帐号,帐户余额为月初,交易类型(提款,入金,利息),交易金额
Input file: customer’s account number, account balance at beginning of month, transaction type (withdrawal, deposit, interest), transaction amount
输出:帐号,期初余额,期末余额,已付利息总额,存款总额,存款,总提款额,提款次数
Output: account number, beginning balance, ending balance, total interest paid, total amount deposited, number of deposits, total amount withdrawn, number of withdrawals
package sentinel;
import java.io.*;
import java.util.*;
public class Ex7 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException
{
int AccountNum;
double BeginningBalance;
double TransactionAmount;
int TransactionType;
double AmountDeposited=0;
int NumberOfDeposits=0;
double InterestPaid=0.0;
double AmountWithdrawn=0.0;
int NumberOfWithdrawals=0;
boolean found= false;
Scanner inFile = new Scanner(new FileReader("Account.in"));
PrintWriter outFile = new PrintWriter("Account.out");
AccountNum = inFile.nextInt();
BeginningBalance= inFile.nextDouble();
while (inFile.hasNext())
{
TransactionAmount=inFile.nextDouble();
TransactionType=inFile.nextInt();
outFile.printf("Account Number: %d%n", AccountNum);
outFile.printf("Beginning Balance: $%.2f %n",BeginningBalance);
outFile.printf("Ending Balance: $%.2f %n",BeginningBalance);
outFile.println();
switch (TransactionType)
{
case '1': // case 1 if we have a Deposite
BeginningBalance = BeginningBalance
+ TransactionAmount;
AmountDeposited = AmountDeposited
+ TransactionAmount;
NumberOfDeposits++;
outFile.printf("Amount Deposited: $%.2f %n",AmountDeposited);
outFile.printf("Number of Deposits: %d%n",NumberOfDeposits);
outFile.println();
break;
case '2':// case 2 if we have an Interest
BeginningBalance = BeginningBalance
+ TransactionAmount;
InterestPaid = InterestPaid
+ TransactionAmount;
outFile.printf("Interest Paid: $%.2f %n",InterestPaid);
outFile.println();
break;
case '3':// case 3 if we have a Withdraw
BeginningBalance = BeginningBalance
- TransactionAmount;
AmountWithdrawn = AmountWithdrawn
+ TransactionAmount;
NumberOfWithdrawals++;
outFile.printf("Amount Withdrawn: $%.2f %n",AmountWithdrawn);
outFile.printf("Number of Withdrawals: %d%n",NumberOfWithdrawals);
outFile.println();
break;
default:
System.out.println("Invalid transaction Tybe: " + TransactionType
+ TransactionAmount);
}
}
inFile.close();
outFile.close();
}
}
但这是给我的:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at sentinel.Ex7.main(Ex7.java:36)
Java Result: 1
推荐答案
我将按照以下步骤进行操作:
This is how I would proceed:
- 在 API中查找该异常的含义文档.
- 考虑它如何发生,发生在哪一行(请查看您的堆栈跟踪).如果您不知道如何读取堆栈跟踪,请这是一个教程.
- 如果您无法立即看到原因,请设置一个断点,并通过调试器运行该断点,以在异常发生之前可视化"程序的当前状态.
您可能还不知道如何使用调试器,尽管有些人可能不同意,但我认为现在还为时过早.在此处上有一些关于如何使用调试器的优秀视频教程.祝你好运!
You might not know how to use your debugger yet, and while some might not agree, I think it's never too early. There are some excellent video tutorials on how to use the debugger here. Good luck!
这篇关于有关使用Scanner时InputMismatchException的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!