0

I'm new to Java and I can't figure out how to solve this problem. Whenever I try to run the program, after putting in a number for the "please enter 5 digit number" entry, the program goes directly to the else statement: "ERROR: Please enter a D, W, B, or X"

Here's the entire code:

import java.util.Scanner;    // needed to use Scanner for input

public class SeminoleBank_A {
 public static void main(String[] args) {

  //Create Scanner object
  Scanner input = new Scanner(System.in);

  //Declare and initialize
  int deposit, withdrawal;
  int balance = 0;
  int number;
  String menuChoice = "";

  //Display welcome message “Seminole Bank”
  System.out.println("\n********************************************************");
  System.out.println("               Welcome to Seminole Bank!                   ");
  System.out.println("********************************************************");

  //Ask user to enter 5-digit Account Number
  System.out.print("Please enter your 5-digit Seminole Account Number: ");

  //Read user's Account Number
  number = input.nextInt();

  //Display "Thank You"
  System.out.println("Thank you!!");

  //Prompt user for year born
  System.out.print("Enter D for deposit, W for withdrawal, B for balance, X to exit the menu: ");

  // Read user's menu choice
  System.out.println(menuChoice.toUpperCase() );

  while (menuChoice != "X") {
        if (menuChoice == "D"){

          System.out.print("Enter the amount of the deposit: ");
          deposit = input.nextInt();
          System.out.printf("Account Number: " + number + "has a current balance of %.2f $: " + (balance + deposit));
        }//end of if
        else if (menuChoice == "W"){

          System.out.print("Enter the amount of the withdrawal: ");
          withdrawal = input.nextInt();
          System.out.printf("Account Number: " + number + "has a current balance of %.2f $: " + (balance + withdrawal));
        }//end of else if
        else if (menuChoice == "B"){

          System.out.printf("Account Number: " + number + "has a current balance of %.2f $: " + balance);
        }//end of else if
        else {

        System.out.println("ERROR: Please enter a D, W, B, or X: ");
        }//end of else


    }//end of while

System.out.println("Thank you for being a loyal Seminole Bank customer!");

}//end of main

}//end of class

Any help would be greatly appreciated.

M. Tango
  • 11
  • 1
  • 3
    And you gathered the menu choice where, exactly? It seems like you printed the menu (under the incorrect comment of year born), and then printed `menuChoice.toUpperCase()`. – KevinO Apr 15 '16 at 04:05
  • 1
    Of course it's not working. The `menuChoice` variable is empty, you didn't read the data. Change `System.out.println(menuChoice.toUpperCase() );` to `menuChoice = input.next().toUpperCase();`, then you can print it `System.out.println(menuChoice);` – developer033 Apr 15 '16 at 04:06
  • In addition to what @developer033 said , you need to ask again for `menuChoice` if you want the user to do it multiple times. Or it will remember what the user put in and keep going there. Additionally, I'd recommend using a Swtich statement instead of the else-if – Norsk Apr 15 '16 at 04:08
  • Please, M. Tango, take a look: https://ideone.com/oiZtf6. – developer033 Apr 15 '16 at 04:31

0 Answers0