I attempted to post this in switch however was unable
Hello,
I am currently learning java programming and have attempted to construct a program that reads a customer's account number (int type), account type (char type; s or S for savings, c or C for checking), minimum balance that the account should maintain and current balance.
The program should then output all four variables stating standing. It is relatively simple, but i chose it as a test example. I believe i have constructed a core that should do everything, but there is something wrong with my coding.
Here is the error message i am receiving :
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextByte(Scanner.java:1924)
at java.util.Scanner.nextByte(Scanner.java:1883)
at account.Account.main(Account.java:32)
Java Result: 1
* in the third program i am receving the error message AccountID may not have been initialized. I dont receive the exception when i removed the switch, and i believe the exception to be related to the switch coding.
I will post my code in two forms. The first form is simply inputting the variables from a txt file that is constructed as
46728 S 1000 2700.
The second code is one i constructed to input the variables manually in an attempt to debug the switch error.
If you do not wish to create a file to be inputed into first code, simply view second code if you are willing to help me find code. I really appreciate any help on this..
Code 1:
==========package account; /** * * @author GStateSwish */ import java.io.*; import java.util.*; public class Account { public static void main(String[] args)throws FileNotFoundException { int accountNum; double currentBalance; int minBalance; char accountType; String outputStr; String outputStr2; String outputStr3; Scanner inFile = new Scanner (new FileReader("C:\\Users\\WES\\Documents\\" + "netbeansfiles\\bankaccount.txt")); accountNum = inFile.nextInt(); accountType = (char) inFile.nextByte(); minBalance = inFile.nextInt(); currentBalance = inFile.nextDouble(); double shortage = currentBalance - minBalance; switch(accountType) { case 's': case 'S': if (currentBalance > minBalance) outputStr = ("Your account, " +accountNum + ", is a Savings"+ " account. Your current account balance is : " + currentBalance) +"which exceeds the minimum required balance of " +minBalance +". Thank you for banking with us!"; else outputStr = "Your account, " +accountNum + ", is a Checking" + "account that is currently below minimum balance. " + "Your account balance is : " + currentBalance + ". Please add $" +shortage +" to achieve minimum required " + "balance. Thank you for your patronage."; System.out.println(outputStr); break; case 'C': case 'c': if (currentBalance > minBalance) outputStr = ("Your account, " +accountNum + ", is a Checking"+ " account. Your current account balance is : " + currentBalance) +"which exceeds the minimum required balance of " +minBalance +". Thank you for banking with us!"; else outputStr = "Your account, " +accountNum + ", is a Checking" + "account that is currently below minimum balance. " + "Your account balance is : " + currentBalance + ". Please add $" + shortage +" to achieve minimum required " + "balance. Thank you for your patronage."; System.out.println(outputStr); break; default: System.out.println("The account type is invalid"); } inFile.close(); } }
Code 2
package javaapplication12; import java.util.*; /** * * @author GStateSwish */ public class JavaApplication12 { /** * @param args the command line arguments */ static Scanner console = new Scanner (System.in); public static void main(String[] args) { // TODO code application logic here int accountNum; double currentBalance; int minBalance; char accountType; String outputStr; String outputStr2; String outputStr3; accountNum = console.nextInt(); minBalance = console.nextInt(); currentBalance = console.nextDouble(); accountType = (char) console.nextByte(); double shortage = currentBalance - minBalance; switch(accountType) { case 's': case 'S': if (currentBalance > minBalance) outputStr = ("Your account, " +accountNum + ", is a Savings"+ " account. Your current account balance is : " + currentBalance) +"which exceeds the minimum required balance of " +minBalance +". Thank you for banking with us!"; else outputStr = "Your account, " +accountNum + ", is a Savings" + "account that is currently below minimum balance. " + "Your account balance is : " + currentBalance + ". Please add $" +shortage +" to achieve minimum required " + "balance. Thank you for your patronage."; System.out.println(outputStr); break; case 'C': case 'c': if (currentBalance > minBalance) outputStr = ("Your account, " +accountNum + ", is a Checking"+ " account. Your current account balance is : " + currentBalance) +"which exceeds the minimum required balance of " +minBalance +". Thank you for banking with us!"; else outputStr = "Your account, " +accountNum + ", is a Checking" + "account that is currently below minimum balance. " + "Your account balance is : " + currentBalance + ". Please add $" + shortage +" to achieve minimum required " + "balance. Thank you for your patronage."; System.out.println(outputStr); break; default: System.out.println("The account type is invalid"); } } }
I have since figured how to implement accountID into and if statement, however i now receive an error that accountID may not be initialized.
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package account; /** * * @author GStateSwish */ import java.io.*; import java.util.*; public class Account { public static void main(String[] args)throws FileNotFoundException { int accountNum; double currentBalance; int minBalance; char accountType; String outputStr; String outputStr2; String outputStr3; Scanner inFile = new Scanner (new FileReader("C:\\Users\\WES\\Documents\\" + "netbeansfiles\\bankaccount.txt")); accountNum = inFile.nextInt(); accountType = (char) inFile.nextByte(); minBalance = inFile.nextInt(); currentBalance = inFile.nextDouble(); String accountID; double shortage = currentBalance - minBalance; if (accountType == 'S') accountID = "Savings"; if (accountType =='s') accountID = "Savings"; if (accountType == 'C') accountID = "Checkings"; if (accountType == 'c') accountID = "Checkings"; if (currentBalance > minBalance) outputStr = ("Your account, " +accountNum + ", is a"+ accountID + " account. Your current account balance is : " + currentBalance) +"which exceeds the minimum required balance of " +minBalance +". Thank you for banking with us!"; else outputStr = "Your account, " +accountNum + ", is a" + accountID + "account that is currently below minimum balance. " + "Your account balance is : " + currentBalance + ". Please add $" +shortage +" to achieve minimum required " + "balance. Thank you for your patronage."; System.out.println(outputStr); inFile.close(); } }