-1
  static boolean check(double money)
  {
    String scont, yes = "yes", no = "no";
    boolean bcont;
    if (money == 0) {
      System.out.println("You are broke and can no longer play.");
      bcont = false;
      return bcont;
    }
    System.out.println("You have " + form.format(money) + " left.");
    System.out.println("Would you like to continue playing? (Yes or no?)");
    scont = in.nextLine();
    if (scont.equalsIgnoreCase(yes)) {
      bcont = true;
      return bcont;
    }
    else if (scont.equalsIgnoreCase(no)) {
      bcont = false;
      return bcont;
    }
    else {
      System.out.println("Invalid answer.");
      bcont = check(money);
      return bcont;
    }
  }

This is, obviously, only a singular function in my program. When it gets to scont = in.nextLine(); it skips the user input and breaks the loop the function is in, in the main function.

Patriot524
  • 201
  • 1
  • 3
  • 7
  • 3
    Are you sure you meant to write `else if (scont.equalsIgnoreCase(no) == false) {` ? – Dawood ibn Kareem Oct 21 '13 at 01:18
  • I suspect the problem might be outside of this particular method. In particular, you may be fetching a number from the `Scanner`, but not fetching the new line following it. Can you post all of your code please, so we can see if this is the problem? – Dawood ibn Kareem Oct 21 '13 at 01:19
  • A side note: `equalsIgnoresCase()` returns a boolean. You don't need to do a comparison on booleans (e.g `equalsIgnoresCase() == true` can just be shortened to `equalsIgnoresCase()`. If you want the conditional block to execute then the method returns `false`, you can use the `!` (not) operator. – Java Devil Oct 21 '13 at 01:21
  • Thanks Java Devil. That sounds like it might be the problem. I will check it out and let you know. – Patriot524 Oct 21 '13 at 01:28
  • It seems that was the issue. Now I have another issue. It will skip the user input once. Then, it will go to the else statement, call itself, and then it will allow for user input. This does not make sense to me at all. – Patriot524 Oct 21 '13 at 01:31

2 Answers2

0

More than likely you're reading the money value from the Scanner which is not consuming the newline characters. In that case add in.nextLine() to consume this character

double money = in.nextDouble();
in.nextLine(); // add
boolean result = check(money);

(Of course BigDecimal is the preferred datatype for monetary amounts)

Reimeus
  • 158,255
  • 15
  • 216
  • 276
0

A few observations:

  • You should always use .equals() in place of == for String comparison in Java
  • But, this is a moot point because equalsIgnoreCase() returns a boolean value anyway; you don't need to perform boolean evaluations in your conditionals
  • I suspect you may be incorrectly making use of the Scanner class, as observed by @David Wallace

It might pay to check your usage of the Scanner class. You can identify whether or not this is the root of your problem by simply using a BufferedReader to temporarily receive the user input.

e.g.

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
...
scont = br.readLine();
x4nd3r
  • 855
  • 1
  • 7
  • 20
  • I attempted using `System.readline()` however no such method exists according to the compiler. Perhaps a class name? I tried using `System.in.readline()` however that did not exist either, however `System.in` does. – Patriot524 Oct 21 '13 at 01:38
  • Sorry, I wasn't thinking straight. See my edited answer. – x4nd3r Oct 21 '13 at 02:44