excedion
excedion

Reputation: 347

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

    }
}

Upvotes: 7

Views: 607

Answers (4)

Andrew Mao
Andrew Mao

Reputation: 36940

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

Upvotes: 13

burmat
burmat

Reputation: 2548

You are pulling the input in as type int, but using String arguments in your cases.

Change your cases to:

case 1:
    // do something
    break;
case 2:
    // do something
    break;
//etc..

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1503419

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.

Upvotes: 8

assylias
assylias

Reputation: 328863

menuSelect is an int whereas '1' is a char which, once converted to an int is 49... Simply remove the quotes and it should work as expected.

Upvotes: 4

Related Questions