-1

I'm a total beginner at Java and am struggling with making a simple card game where you choose between values "red" and "black". Can anyone see what's wrong?

String guess;
Scanner keyboard = new Scanner(System.in);

Random r = new Random();
char answear = r.nextBoolean() ? 'R' : 'B';
String s = String.valueOf(answear);

do {
    System.out.println("Guess the color of the card - (R)ed or (B)lack?");
    guess = keyboard.next();
    if (guess == s)
        System.out.println("Correct");
    else
        System.out.println("Wrong");
} while (guess != s);
0xCursor
  • 2,242
  • 4
  • 15
  • 33
Mathayas
  • 33
  • 3
  • replace `keyboard.next()` with `keyboard.next().trim()` – Iłya Bursov Jul 30 '18 at 18:25
  • 2
    also replace `guess == s` with `s.equals(guess)` – Iłya Bursov Jul 30 '18 at 18:26
  • You are converting the `char` to a `String` (`String s = String.valueOf(answear);`) then using `==` to compare it (`if (guess == s)`). You need to use `.equals()` to compare strings. – xtratic Jul 30 '18 at 18:27
  • Check out [this post](https://stackoverflow.com/questions/7520432/what-is-the-difference-between-vs-equals-in-java) for info on comparing `String`s in Java. Other than that, your code seems fine. Note: hopefully you are closing your `Scanner` somewhere like: `keyboard.close();`. – 0xCursor Jul 30 '18 at 18:32

3 Answers3

3

In Java Strings are an object, when comparing Objects:

== tests for reference equality (whether they are the same object).

.equals() tests for value equality (whether they are logically "equal").

This is further explained in How do I compare strings in Java?

On the other hand, char is a primitive data type and on primitive types == tests for value equality. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Edit - Regarding case:

In Java you have the method "equalsIgnoreCase" to compare two Strings irrespective of the case (lower or upper) of the string. This method returns true if the argument is not null and it represents an equivalent String ignoring case, else false.

1

So, you've got a couple of problems here:

String guess;
Scanner keyboard = new Scanner(System.in);

Random r = new Random();
char answear = r.nextBoolean() ? 'R' : 'B';
String s = String.valueOf(answear);

do {
    System.out.println("Guess the color of the card - (R)ed or (B)lack?");
    guess = keyboard.next();
    //The problem is that you're using reference comparison, but that's not the only issue you 'will have', you're only checking raw input vs upper case 'R' 
    //and 'B' so you 'need' (you can handle this with lower case or alternative, too) to change this to `.toUppercase()`.
    if (guess == s)
        System.out.println("Correct");
    else
        System.out.println("Wrong");
//The same problem is happening here:
} while (guess != s);

New code:

String guess;
Scanner keyboard = new Scanner(System.in);

Random r = new Random();
char answear = r.nextBoolean() ? 'R' : 'B';
String s = String.valueOf(answear);

do {
    System.out.println("Guess the color of the card - (R)ed or (B)lack?");
    guess = keyboard.next();
    if (guess.toUpperCase().equals(s))
        System.out.println("Correct");
    else
        System.out.println("Wrong");
} while (!guess.toUpperCase().equals(s));}

When you use ==, you're comparing references.

To compare text (string type), you should use .equals().

You should also make sure that the case upper/lower is catered for to prevent errors, as r is not equal to R.

0xCursor
  • 2,242
  • 4
  • 15
  • 33
  • 2
    `When you use == you're comparing integers` it is wrong statement – Iłya Bursov Jul 30 '18 at 20:31
  • Explain your comment don't just shotgun something, explain why - your comment doesn't add anything useful. –  Jul 30 '18 at 21:32
  • 2
    `==` here is used to compare references (which are not integers per se) – Iłya Bursov Jul 30 '18 at 21:33
  • Ilya Bursov is correct about the `==`. Also, you should use `.equals()` in the `while` check statement like: `while (!guess.toUpperCase().equals(s));` because right now it will loop forever. – 0xCursor Jul 31 '18 at 18:46
  • 1
    @LAD answer was updated to reflect that point, thanks for fixing the typo in my other answer too –  Jul 31 '18 at 22:24
0

Basically, don't use == for strings, that's only good for primitive data types or basically anything that isn't an object. Because that points to the reference of the string and compares if they're equal (Basically testing if they're the same variable not the same string contents. In the case of strings you want to use .equals(), Usually I put a ToUpperCase() statement in there like so:

if(guess.toUpperCase().equals(s.toUpperCase()))

to get rid of case sensitivity because it just makes everyones lives easier but I know thats not a part of the question

tldr: Use .equals when comparing strings, otherwise it points to the objects of the string and not the contents.