1
public void humanPlay()
 {
if (player1.equalsIgnoreCase("human"))
    System.out.println("It is player 1's turn.");
else
    System.out.println("It is player 2's turn.");

System.out.println("Player 1 score: " + player1Score);
System.out.print("Player 2 score: " + player2Score);

String eitherOr;

  do {
    eitherOr= input.nextLine(); 
    humanRoll();
  } while (eitherOr.isEmpty());

 if (!eitherOr.isEmpty())
    humanHold();

}

This is the whole method, The only thing i am trying to fix is this.

       String eitherOr;
do {
     eitherOr= input.nextLine();    
     humanRoll();
   } while (eitherOr.isEmpty());

It has to accept input multiple times, so each time input is needed to determine what happens, which is why i like the Do While loop, but since it initializes at least once per time, i get an extra roll than needed.

I have tried to do it this way, and various variations of this way:

String eitherOr = input.nextLine();

while(eitherOr.isEmpty());
        humanRoll();

This does not work because it does not ask for the input over again. If i try to put the input.nextline(); into the while loop, it says "eitherOr" is not initialized, and even if i initialize it when i enter input, the command line stays blank, so it does nothing with my input.

2 Answers 2

4

You've got an extraneous semi-colon:

while(eitherOr.isEmpty());
    humanRoll();'

should be:

while(eitherOr.isEmpty())
    humanRoll();

Essentially your version is saying to do nothing while eitherOr.isEmpty() is true and so it never gets to call humanRoll.

Sign up to request clarification or add additional context in comments.

3 Comments

Well then, it seems like the last hour has been wasted on a semi colon. Thank you, I have not used a while-loop in a while. Due to this misfortune I foresee me re-reading a few chapters. Probably should have been the first thing i checked to be honest.
@ LanceySnr The code still works like it did while it was a do while loop, even if there is input, it still creates another roll.
Have you used a debugger or similar to check the content of it when there is input? Given the code posted I can't see how it could execute the loop.
1

If your second code snippet you are executing a blank statement as part of your while loop

while(eitherOr.isEmpty());//this semicolon is a blank statement
    humanRoll();

you have to remove this semicolon in order to execute humanRoll as part of loop

while(eitherOr.isEmpty())
    humanRoll();

On a side note use of paranthesis generally avoids such minor issue

while(eitherOr.isEmpty()) {
    humanRoll();
}

In above code its easy to identify if an unintentional semicolon has been introduced or not.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.