0

how do you restart game in java?

this is what i tried but did not work:

String playagain;
    String y="y";
    String n="n";

//there is a huge chunk of code that goes in here for the game

System.out.println("play another game?(y/n)");
    playagain=input.next();
    if(playagain==y){
        Game.end();
        Game.start();
    }else if(playagain==n){
        System.out.println("Goodbye");
    }

game is the name of the class, but it insists i create a method for that to work. would anyone know an easier way to restart the game when player clicks y? Any help is appreciated

droidnoob
  • 333
  • 5
  • 17
  • 2
    Oh lol ==y? You mean playagain.equals("y") and .equals("n") – Ya Wang Oct 02 '15 at 18:24
  • There should really be more information in your question. Does `Game` have static methods `end` and `start`? Do they do what you want them to? And indeed, don't compare strings with `==`, and I really doubt that you have variables named `y` and `n` that would make sense for this comparison anyway. – RealSkeptic Oct 02 '15 at 18:27
  • yes i have variables for y and n – droidnoob Oct 02 '15 at 18:28
  • 1
    Just a heads up, I THINK the down votes are because we're not seeing the exact compiler error that you're getting. Don't just state that something didn't work, also; state what happened, and what was expected instead. It was good to share what you're tried, though! – Luke Oct 02 '15 at 18:31
  • @ya wang there are strings y and n in the program which equlas "y" and "n" respectively – droidnoob Oct 02 '15 at 18:31
  • @realSkeptic i do have variables for both, but i don't have end or start – droidnoob Oct 02 '15 at 18:32
  • @luke thanks i will edit to show more – droidnoob Oct 02 '15 at 18:33
  • http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Ya Wang Oct 02 '15 at 18:35
  • So if you don't have those methods, how do you expect them to work? – RealSkeptic Oct 02 '15 at 18:35
  • Its a question, I'm trying to learn it, I do not know. If you know could you help? – droidnoob Oct 02 '15 at 18:37

2 Answers2

1

I suspect two things are happening in your original code:

  1. You're using y and n, rather than "y" and "n" - the former would look like variable names to the compiler, whereas the latter are string literals, which I think we're after. If you're actually meaning the former, we'd need to declare some variables named y and n as Strings, give them some values (probably "y" and "n" respectively), although I don't think it's worth using variables in this case (given that they're only going to be referenced once in your code, no complex expressions, not mutable.

  2. If you simply use == when comparing strings, Java will actually check whether the strings have the same reference (ie they're the exact same instance, the same object). If you're wanting to check the contents of the string, we need to evaluate string1.equals(string2), which will be true if they have the same content and false otherwise. Bit more information here.

EDIT: Okay, the problem is deeper than I first thought. Consider structuring your code like this instead of the above (if you don't have those start and end methods):

while (true) {
    // Play the game here
    // Play again?
    boolean isPlayingAgain = true;
    while (true) {
        System.out.println("play another game?(y/n)");
        String playingAgainResponse = input.next();
        if (playingAgainResponse.equalsIgnoreCase("y")) {
            break;
        } else if (playingAgainResponse.equalsIgnoreCase("n")) {
            System.out.println("Goodbye");
            isPlayingAgain = false;
            break;
        }
    }
    if (!isPlayingAgain) {
        break;
    }
}

So the outer loop only terminates if we player doesn't want to play again, otherwise we keep running the code to play the game. That inner loop ensures that the player enters either a "y" or a "n" (in upper or lower case, actually), and so it is handling the case where the player enters some gibberish.

Community
  • 1
  • 1
Luke
  • 1,724
  • 1
  • 12
  • 17
  • String y="y" and String n="n" – droidnoob Oct 02 '15 at 18:30
  • Yes, but `==` checks for reference as Luke already said, `ie they're the exact same instance, the same object`. Just use `playagain.equals(y)`. – Hatted Rooster Oct 02 '15 at 18:31
  • 1
    I'm not sure if there is any point in using variables here? Perhaps just use `playagain.equals("y")`, etc. Small style note, too; Java convention would probably prefer the variable name `playAgain` (note the lower camel case). – Luke Oct 02 '15 at 18:33
  • @Luke ok I have edited my code, what do you suggest i put in place of the Game.start(), because I do not actually have a working method of that in my code – droidnoob Oct 02 '15 at 18:39
  • Edited my response based on this new info :) – Luke Oct 02 '15 at 18:47
  • it worked! thank you so much! – droidnoob Oct 02 '15 at 19:01
0

Considering that playagain is a String and you get a input from the user with input.next();, then you should edit your condition :

if(playagain.equalsIgnoreCase("y")){
    Game.end();
    Game.start();
}else if(playagain.equalsIgnoreCase("n")){
    System.out.println("Goodbye");
}

if n and y are variable containing a String, do as below

if(playagain.equalsIgnoreCase(y)){
    Game.end();
    Game.start();
}else if(playagain.equalsIgnoreCase(n)){
    System.out.println("Goodbye");
}
Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
Benoit
  • 208
  • 3
  • 13