0

I'm trying to make a simplified version of Black Jack in Java using eclipse. I'm trying to make it so the player types 'hit' or 'stand', and while they haven't, it keeps prompting them to do so.

while (hitorstand != ("hit") || hitorstand != ("stand"))
                {
                    System.out.println("Would you like to hit or stand?(1 for hit, 2 for stand)");
                    hitorstand = scan.nextLine();
                    hitorstand.toLowerCase();
                }       
                if (hitorstand.equals("hit"))
                    {
                        playercard3 = random.nextInt(10) +2;
                        System.out.println(""+playercard3);
                    }
                else if (hitorstand.equals("stand"))
                    {
                        System.out.println("You had a total value of " + playercardtotal + ".");
                        if (hiddendealercard == card2)

When I run it, no matter what I type it cannot escape the while loop. I know it would work if I used numbers but I really want to learn how to use words as input.

Trevor M
  • 157
  • 1
  • 11

5 Answers5

4
while (hitorstand != ("hit") || hitorstand != ("stand")) // This is not the right way

Use the equals() method for String value comparison. == is for object reference comparison.

while (!hitorstand.equals("hit") || !hitorstand.equals("stand")) // This is

I'm not sure why you'd use the != in the while loop condition, whereas you've properly used (hitorstand.equals("hit")) just below the while, in a if statement.

Also, there seems a minor mistake in the while loop block.

hitorstand.toLowerCase(); // This does nothing

As Strings are immutable in java, you need to assign back the changed string to be able to see the changes

hitorstand = hitorstand.toLowerCase(); // Assigning back the lowercase string back to hitorstand 
Rahul
  • 44,383
  • 11
  • 84
  • 103
  • The while (!hitorstand.equals("hit") || !hitorstand.equals("stand")) gives me the error Exception in thread "main" java.lang.NullPointerException at expert.Expert.main(Expert.java:232) – Trevor M Oct 18 '13 at 17:53
  • AFAIK, I won't and doesn't give any error. Can you post what error you're getting? – Rahul Oct 18 '13 at 17:56
  • It seems you're not initializing your string before it comes to the while. In that case, just put this statement before the while loop. `String hitorstand = "";` – Rahul Oct 18 '13 at 17:57
2

You need to use .equals(..) instead of ==. This is because == is used for reference equality, while .equals() is simply for value equality.

For example:

while(!hitorstand.equals("hit") || !hitorstand.equals("stand"))
Andrew Backes
  • 1,884
  • 4
  • 21
  • 37
0

Comparing hitorstand != ("hit") you actually compare object references not the String value itself. To compare strings you need to use equals method. In java every class inherits equals ( from Object ) and it can be overriden for custom object comparison

Try this:

while (!hitorstand.equals("hit") || !hitorstand.equals("stand")){
Luke
  • 1,236
  • 2
  • 11
  • 16
0

Adding to the answers, a good rule of thumb is to use .equals() with strings and == with integer values or variables with integer values (or the value null).

dtgee
  • 1,272
  • 2
  • 15
  • 30
0

One way you could do this would be to use a character. For example: instead of
while (hitorstand != ("hit") || hitorstand != ("stand"))
you could have it check for the first character in the string using the charAt() command with the index of the string in the parenthesis. So since your looking for the first character, it would be at index 0.
while (x != 'h' || x != 's')
x being a char.

Inside your while loop,
System.out.println("Would you like to hit or stand?");
hitorstand = scan.nextLine();
hitorstand.toLowerCase();
x = x.charAt(0); // you would just add this line. This gets the character at index 0 from the string and stores it into x. So if you were to type hit, x would be equal to 'h'.

Your if statement could stay the same or you could also change the condition to (x == 'h') and (x == 's'). That's up to you.

Oscar F
  • 323
  • 2
  • 9
  • 21