Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java, weird switch statement behaviour

trawling through a few programming assignments (first year) and ive come up with this. when i run it, the program goes straight to the default on the switch statement. been up for about 24 hours straight now so im hardly aware but i just can't find whats going wrong.

Could anyone point me in the write direction. I don't want you to do it for me

// Practical 5B - Question 1
// Nathan Griffin
// 28/02/2013
// Program to simulate a continuos system of deposits and withdraw for a bank account until user exits.

import java.util.Scanner;
public class SingleAccountSim
{
    public static void main(String [] args)
    {
        Scanner keyboardIn = new Scanner(System.in);

        BankAccount account = new BankAccount();

        int menuSelect = 0;
        double depositIn, withdrawalOut;



        do
        {
            System.out.println("*_*_*Monopoly Bank*_*_*");
            System.out.println("1. Deposit money");
            System.out.println("2. Withdraw money");
            System.out.println("3. Get balance");
            System.out.println("4. Quit");

            menuSelect = keyboardIn.nextInt();

            switch(menuSelect)
            {
                case '1':   System.out.print("Please enter the deposit amount: ");
                                depositIn = keyboardIn.nextDouble();
                                account.deposit(depositIn);
                                break;              
                case '2': System.out.print("Please enter the withdrawl amount: ");
                                withdrawalOut = keyboardIn.nextDouble();
                                account.withdraw(withdrawalOut);
                                break;
                case '3': System.out.print("Your current balance is " + account.getBalance());
                                break;
                case '4': System.out.print("Quitting.....");
                                break;
                    default: System.out.println("Invalid selection. Please try again");
            }
        }while(menuSelect !=4);

    }
}
like image 391
excedion Avatar asked Feb 28 '13 17:02

excedion


2 Answers

Well, you are comparing an int (due to menuSelect = Scanner.nextInt()) with a char, so of course it won't match. In the switch statement, the character '1' is equivalent to a value of 49 when assigned to an int. From the switch statement in the Java Language Specification, it is a little known fact that

Every case constant expression associated with a switch statement must be assignable (§5.2) to the type of the switch Expression.

For example, the following (valid, surprisingly) code prints 49:

int a = '1';
System.out.println(a);

So the case constant and the switch(expression) need not be the same type, and this won't generate a compilation error for primitive types. You can fix this by using

switch(menuSelect) {
    case 1:
    ...
    case 2:
    ...
}

You can also read a char in instead of an int, and keep things the way they are. But Scanner doesn't have a nextChar() method, so you'd have to use nextByte() and convert it.

char menuSelect = (char) keyboardIn.nextByte()
like image 78
Andrew Mao Avatar answered Oct 09 '22 04:10

Andrew Mao


You're fetching an int value, but then comparing it with a char value. The char values in the switch statement are being promoted to the corresponding int.

So actually, if you type in 49 (the Unicode value for '1'), it will print "Please enter the deposit amount" etc.

Just change

case '1':

to

case 1:

etc.

like image 38
Jon Skeet Avatar answered Oct 09 '22 04:10

Jon Skeet