Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Can you spot the error in my switch?

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    13
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Can you spot the error in my switch?

    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();
        }
    }


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Can you spot the error in my switch?

    InputMismatchException means that you are asking the scanner to read and convert what it finds as a particular type (int, double, whatever) but it is finding something that cannot be interpreted as that sort of thing. For instance if you say nextInt() but the scanner is looking at the string "foo" in the file, it will have a hard time returning an int and will throw the InputMismatchException instead.

    This is happening on line 32 of the main() method. (according to the stack trace you posted). So which is line 32?

    ---

    It occurs to me that a byte is not a char. Scanner doesn't have a nextChar() and you can't use nextByte() because that will be looking for a numeral in the text file. Something like "42", not something like "S".

    Notice that in your text file the "S" appears as a complete token (aka word, aka string-without-whitespace-in-it) so you could read it as a String with next(). Then, to set the value of accountType, you would find the first (and only) char of that String using one of the String methods that does that.

    ---

    Small point, but your formatting is all over the place. Do you (or your text editor/IDE) mix spaces and tabs? Personally I think spaces are a lot safer when code is going to be reproduced somewhere else - like here (and my web browser) - that doesn't make the same assumptions about how big a tab is.

  3. The Following User Says Thank You to pbrockway2 For This Useful Post:

    GStateSwish (July 4th, 2013)

  4. #3
    Junior Member
    Join Date
    Jul 2013
    Posts
    13
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Can you spot the error in my switch?

    Thank you for your response. I was able to fix the program with your suggestions.

    originally line 32 was the declaration:
    accountType = (char) inFile.nextByte();

    I do also realize my coding is not well structured, but i assume this will improve with experience.
    Here is the fixed code.

    ****not sure why the code isnt working under syntax highlighting, however it is now functioning properly again thanks for the help****

    package account;
     
    /**
     *
     * @author GStateSwish *assistance from pbrockway2
     * 
     * This program 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 then 
     * outputs the account number, type, current balance, and an appropriate 
     * message.
     */
    import java.io.*;
    import java.util.*;
     
    public class Account {
     
        public static void main(String[] args)throws FileNotFoundException {
     
        int accountNum;
        double currentBalance;
        int minBalance;
        String accountType;
        String outputStr;
     
        Scanner inFile = new Scanner (new FileReader("C:\\Users\\WES\\Documents\\"
                + "netbeansfiles\\bankaccount.txt"));
     
        accountNum = inFile.nextInt();
        accountType = inFile.next();
        minBalance = inFile.nextInt();
        currentBalance = inFile.nextDouble();
     
        double shortage = Math.abs(currentBalance - minBalance);
        char accountChar = (accountType.charAt(0));
     
        switch(accountChar)
        {
            case 's':
            case 'S':
     
      if (currentBalance > minBalance)
     
           outputStr = "Your account," +accountNum + ", is a Savings"+ 
                   " account.\nYour current account balance is : $" 
                   + String.format("%.2f",currentBalance)
                   +"\nwhich exceeds the minimum required balance of " +minBalance 
                   +".\nThank you for banking with us!";
      else 
     
          outputStr = "Your account," +accountNum + ", is a Savings"
                  + " account that is currently below minimum balance. \n"
                  + "Your account balance is : $"
                  + String.format("%.2f", currentBalance)
                  + ". Please add $" 
                  + String.format("%.2f",shortage) +" to achieve minimum required "
                  + "balance.\n Thank you for your patronage.";
     
                System.out.println(outputStr);
                  break;
     
            case 'C':
            case 'c':
     
        if (currentBalance > minBalance)
     
            outputStr = "Your account," +accountNum + ", is a Checkings"+ 
                   " account.\nYour current account balance is : $" +
                   String.format("%.2f",currentBalance)
                   +"\nwhich exceeds the minimum required balance of " +minBalance 
                   +".\nThank you for banking with us!";
      else 
     
            outputStr = "Your account," +accountNum + ", is a Checkings"
                  + " account that is currently below minimum balance. \n"
                  + "Your account balance is : $"
                  + String.format("%.2f", currentBalance)
                  + ". Please add $" 
                  + String.format("%.2f",shortage) +" to achieve minimum required "
                  + "balance.\nThank you for your patronage.";
     
                System.out.println(outputStr);
                  break;
     
            default:
                System.out.println("The account type is invalid");
        }
        inFile.close();
        }
    }
    Last edited by pbrockway2; July 4th, 2013 at 04:27 PM. Reason: edited code tags

  5. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Can you spot the error in my switch?

    You're welcome. The code tags are [code] ... [/code]

Similar Threads

  1. Focus is in the wrong spot
    By walker6o9 in forum AWT / Java Swing
    Replies: 0
    Last Post: January 15th, 2013, 11:51 PM
  2. error when switch activity in android
    By yefta in forum Android Development
    Replies: 3
    Last Post: October 26th, 2011, 10:50 AM
  3. How to detect brightest spot among all spots?
    By BharatT in forum Java Theory & Questions
    Replies: 4
    Last Post: February 6th, 2009, 09:12 PM