Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Hylekmi

macrumors regular
Original poster
Dec 8, 2010
101
0
Code:
public static void playAgain()
	{
	Scanner console = new Scanner (System.in);
	String decision;
	System.out.println("Do you wish to play again?");
	decision=console.nextLine();
	if(decision=="y"||decision=="Y")
		System.out.println("YES");
	if(decision=="N"||decision=="n")
		System.out.println("NO");
	
		/*do{outputResults(rollDie());}
		while(==true);
		*/
	}//end playagain

I get "do you wish to play again" no matter what i enter.
 
Code:
public static void playAgain()
	{
	Scanner console = new Scanner (System.in);
	String decision;
	System.out.println("Do you wish to play again?");
	decision=console.nextLine();
	if(decision=="y"||decision=="Y")
		System.out.println("YES");
	if(decision=="N"||decision=="n")
		System.out.println("NO");
	
		/*do{outputResults(rollDie());}
		while(==true);
		*/
	}//end playagain

I get "do you wish to play again" no matter what i enter.

I JUST started Java (my 3rd week in lol) but I don't think you can compare strings using == because Strings are references (addresses?) so you're saying

if ( address1 == address2 )

I think you can use if (decision.equals("y") || decision.equals("Y"))

worth a shot until someone more experienced can help ya
 
"Do you wish to play again?" will always display, it's not conditional. Other than that, you're trying to do String comparison with ==, which will only test if that the objects are at the same address. Use .equals to test if the contents are the same instead.

-Lee
 
I JUST started Java (my 3rd week in lol) but I don't think you can compare strings using == because Strings are references (addresses?) so you're saying

if ( address1 == address2 )

I think you can use if (decision.equals("y") || decision.equals("Y"))

worth a shot until someone more experienced can help ya

Your exactly right. Im shot tonight. Eclipse doesn't catch that. thanks
 
Eclipse won't catch it because its valid Java ;). Sometimes you do want to know if something is at the same address.

Or you might be using interned strings which allows this sort of comparison (useful as it saves memory from lots of copies of the same string and allows fast comparison, worth considering if you read lots of strings from a file that might all be the same).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.