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?