-1

Possible Duplicate:
How do I compare strings in Java?

I wrote a program that will "roll dice" and tell you how many turns it takes to get yahtzee. This works, but there is a problem when it asks you to go again. It will keep giving me an "Invalid input", when in fact I enter something that should exit the loop. Here is my code, the loop goes from lines 24-34 but I'll put all of it just in case it is a problem earlier on.

import java.util.Scanner;
import java.util.Random;

public class Yahtzee {

public static void main(String[] args){

    int d[] = {0,0,0,0,0};
    int x = 0;
    Scanner s = new Scanner(System.in);
    Random r = new Random();

    while (1!=0){
        d[0] = r.nextInt(6);
        d[1] = r.nextInt(6);
        d[2] = r.nextInt(6);
        d[3] = r.nextInt(6);
        d[4] = r.nextInt(6);
        x+=1;

        if (d[0]==d[1] && d[0]==d[2] && d[0]==d[3] && d[0]==d[4]) {
            System.out.println("You got yahtzee, every dice showed " + Integer.toString(d[0]));
            System.out.println("It only took " + Integer.toString(x) + " turns");
            while (1!=3) {
                System.out.print("Go again? y/n: ");
                String ans = s.nextLine();
                if (ans.toLowerCase()=="n"){
                    System.exit(0);
                } else if (ans.toLowerCase()=="y") {
                    break;
                } else {
                    System.out.println("Invalid input!");
                }
            }

        }   
    }   
}   
}

I honestly cannot figure this out, although it is probably incredibly obvious. Where is the problem?

Community
  • 1
  • 1
WillumMaguire
  • 537
  • 2
  • 10
  • 21
  • Use of `==` for strings is incorrect. –  Oct 04 '12 at 00:30
  • "Here is my code, the loop goes from lines 24-34 but I'll put all of it just in case it is a problem earlier on." - don't do that, create a reduced code sample. I.e. remove parts you don't think are relevant to the problem and test after every removal to see if the problem still appears. – millimoose Oct 04 '12 at 00:31
  • http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java –  Oct 04 '12 at 00:31
  • Sorry about that. Sometimes with java if people need my help it's easier to just copy+paste their code to find the error, bad habit. – WillumMaguire Oct 04 '12 at 00:33
  • Also yea I forgot about .equals(), too much python I guess. – WillumMaguire Oct 04 '12 at 00:33

3 Answers3

3

Use .equals to compare strings, rather than the equality operator ==.

if (ans.toLowerCase().equals("n")){
    System.exit(0);
} else if (ans.toLowerCase().equals("y")) {

The equality operator checks only that their memory location is equal, which it is not in this case.

FThompson
  • 28,352
  • 13
  • 60
  • 93
1

Use String.equals for checking string content. The == operator relies on reference equality so the first 2 if statement expressions will never be true. You could have:

if (ans.toLowerCase().equals("n")) {
   System.exit(0);
} else if (ans.toLowerCase().equals("y")) {
   break;
} else {
   System.out.println("Invalid input!");
}
Reimeus
  • 158,255
  • 15
  • 216
  • 276
0

Both of the previous answers are correct, you must use the String class' .equals method.

The == operator in your program is actually comparing the 2 String object references not the contents of the Strings.

This is a pretty common mistake for people who are starting out in Java.

See here for more info: http://www.java-samples.com/showtutorial.php?tutorialid=221

Doddie
  • 1,393
  • 13
  • 21