1

I've been trying to figure out why my code no longer goes into my if-statement after the user decides whether or not they want to withdraw or deposit from their account.

Here's my current output:

Would you like to withdraw or deposit money? 
132.0
The date the account was created: Sun Sep 06 22:28:46 EDT 2015

When it gets to the "would you like to withdraw or deposit money?" it no longer allows the user to input any text. So my question is, what can I do to correct this?

Here is snippet of my current code:

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

        System.out.println("Enter the annual interest rate: ");
        interestRate = scan.nextDouble();

        System.out.println("Would you like to withdraw or deposit money? ");
        String input = scan.nextLine();

        if (input.equals("withdraw")) {
            System.out.println("Enter the amount you would like to withdraw: ");

        } else if (...) {
          ...
          }

    } // End of main method.

} // End of class header.
mur7ay
  • 773
  • 3
  • 15
  • 36
  • 2
    Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses – MadProgrammer Sep 07 '15 at 02:58
  • I accidentally submitted the question before I was done. – mur7ay Sep 07 '15 at 03:05
  • I don't see a for-loop in your code – MadProgrammer Sep 07 '15 at 03:16
  • I meant if*. I just changed it everywhere it said for. – mur7ay Sep 07 '15 at 03:17
  • 2
    OK, but `if` is not a loop and there is no such thing as an _if-loop_. _if-statement_ would be better. I'm removing the `loops` tag. – ajb Sep 07 '15 at 03:22

2 Answers2

1

The documentations on nextLine() states that:

Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line. Since this method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present.

So, what happens in your case is that since you have not called any nextLine() before your line :

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

the JVM considers the previous line seperator you have entered to be the line you require to read. The solution to your problem could be you should call nextLine() at-least once to clear the buffer before your line:

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

Like this:

scan.nextLine();    
System.out.println("Would you like to withdraw or deposit money? ");
String input = scan.nextLine();
Blip
  • 2,711
  • 3
  • 17
  • 42
  • Thanks! that worked. I don't think I'm calling the withdraw or deposit method correctly because after it goes into the if statement and I either enter withdraw or deposit and then enter the amount its just printing the balance with interest. – mur7ay Sep 07 '15 at 03:33
  • I figured it out. I wasn't writing to the methods correctly. – mur7ay Sep 07 '15 at 11:45
0

your code looks good .but problem is not in code .while giving inputs to your program .the below step you need enter text "withdraw" or "deposit" but ur entering the number .

String input = scan.nextLine()

input should be withdraw or deposit

if (input.equals("withdraw")) {} //this ur logic saying

but your not giving the correct input to ur program like withdraw or deposit

Would you like to withdraw or deposit money? 
132.0
yugi
  • 732
  • 13
  • 25