0

I am writing an ATM program and when the user inputs one of the string values the program should check it an do a method accordingly. The problem code is here:

System.out.println("PRESS");
            System.out.println("(D)eposit");
            System.out.println("(W)ithdraw");
            System.out.println("(C)heck Account Balance");
            System.out.println("(Q)uit");
            System.out.println("Enter Choice: ");
            String choice = scanner.nextLine();
            scanner.nextLine();
            if(choice == "D"){
                currentCustomer.deposit();
            }
            else if(choice == "W"){
                currentCustomer.withdraw();
            }
            else if(choice == "C"){
                currentCustomer.checkBalance();
            }
            else if(choice == "Q"){
                currentCustomer.quit();
            }
            else{
                System.out.println("Invalid choice please reenter: ");
            }

If a user enters in "D" the program skips to the else statement. I know when using .nextLine you have to use two because of the return character but I'm not sure if that is true with this case. Either way if I have the extra .nextLine statement or not it still skips ahead. Any help would be much appreciated!

Ryan Sayles
  • 3,389
  • 11
  • 56
  • 79

4 Answers4

6

In Java we compare strings with String#equals.

I'll not write the difference between equals and ==, google for more information. You'll get around 100 results.

Maroun
  • 94,125
  • 30
  • 188
  • 241
1

You would be better off using if(choice.equals("D")) in your code. You can't compare strings with == because you are just checking the memory and not the actual contents.

DotNetRussell
  • 9,716
  • 10
  • 56
  • 111
  • Actually you're checking a Reference (memory address) equality. – Warren P May 05 '13 at 20:46
  • @WarrenP You are correct. I couldn't remember if it was a type or an address it was checking. Been a while since my Java Object Oriented class haha. Thanks I will make edit – DotNetRussell May 05 '13 at 20:48
  • I think it's insane that the developers of Java overloaded `+` to combine strings, but not `==`. When is that behaviour ever nice? – Warren P May 05 '13 at 20:49
  • @WarrenP Well it's 100 times worse doing nothing BUT java in college but then going to a DotNet shop to work. Imagine finding out that there is no such thing as a string object in C# and that == works... Mind freaking blown... – DotNetRussell May 05 '13 at 20:53
0

Instead of using String in comparison part:

else if(choice == "C"){
                currentCustomer.checkBalance();
            }

you could use char comparison instead

else if(choice[0] == 'C'){
                currentCustomer.checkBalance();
            }
sha1
  • 153
  • 7
0

You shouldn't be using the == operator to compare strings, use the String equals method instead. The operator checks to see if both strings are stored at the same location in memory while the method checks if they have the same content.

If you're using Java 7, you might want to switch out that if-elseif-then block with a switch statement. Java 7 introduced the ability to use strings in switch statements.

Varun Madiath
  • 3,152
  • 4
  • 30
  • 46