0

so I'm trying to ask the user to input value for control right after the SOP is done. But it skips it for some reason.

public static void main(String [] args)
      {
         Scanner keyboard = new Scanner(System.in);

         System.out.println("What's your balance?");
         double initialBalance = keyboard.nextDouble();

         Account chase = new Account(initialBalance);

         System.out.println(chase + "; Would you like to deposit or withdraw?");
         String control = keyboard.nextLine();

         if(control == "deposit")
         {
            double deposit = keyboard.nextDouble();
            System.out.println("How much would you like to deposit? " +
                                 deposit);
            chase.deposit(deposit);
            System.out.println(chase);                     

         }

      }
PTheCoolGuy
  • 63
  • 1
  • 7

2 Answers2

0

New Code:

public static void main(String [] args)
{
   Scanner keyboard = new Scanner(System.in);

   System.out.println("What's your balance?");
   double initialBalance = keyboard.nextDouble();

   Account chase = new Account(initialBalance);

   System.out.println(chase + "; Would you like to deposit or withdraw?");

   String control2 = "deposit";
   boolean control = keyboard.next().equalsIgnoreCase(control2);

   if(control == true)
   {
      double deposit = keyboard.nextDouble();
      System.out.println("How much would you like to deposit? " +
                           deposit);
      chase.deposit(deposit);
      System.out.println(chase);                     

   }

} }

Cronical
  • 34
  • 8
0

Here is a snippet of your code with the changes required.

         Scanner keyboard = new Scanner(System.in);

        System.out.println("What's your balance?");
        double initialBalance = keyboard.nextDouble();
        keyboard.nextLine();

        Account chase = new Account(initialBalance);

        System.out.println("; Would you like to deposit or withdraw?");
        String control = keyboard.nextLine();

        if (control .equals("deposit") ){

        System.out.println("How much would you like to deposit? " );
        double deposit = keyboard.nextDouble();
        keyboard.nextLine();

The comments are clear. You would need to see the two post that they pointed to. Briefly there is a line separator that comes in and the importance of the equals method and its significance.

000
  • 26,951
  • 10
  • 71
  • 101
Khanna111
  • 3,627
  • 1
  • 23
  • 25