Question about InputMismatchException while using Scanner
Posted
by aser
on Stack Overflow
See other posts from Stack Overflow
or by aser
Published on 2010-04-25T12:25:31Z
Indexed on
2010/04/25
13:33 UTC
Read the original article
Hit count: 259
The question :
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();
}
}
But is gives me this :
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
© Stack Overflow or respective owner