While Loop Not Terminating With Boolean Value Changing - java

A working example is to have a while loop that continues to get a number from the user until they guess the number correctly, then it will return true and exit out the while loop.
However, if I create a method that returns whether the variable is true or false, the while loop does not terminate. But, if the IF condition is inside the while loop, it terminates and work as intended.
Code that Works as Intended:
private void print() { // Method name not decided yet
boolean playerGuessedCorrectly = false;
while(!playerGuessedCorrectly) {
[...] // Other code that asks the user to enter a new number
// userGuess is the next int entered by the user
// computerNumber is a random number between 1 and 100
if(userGuess == computerNumber) {
System.out.print("You have guessed my number.");
playerGuessedCorrectly = true;
} // while loop terminates upon true
}
}
This code works and the while loop stops working when it is changed to true. However, when I put the same code in a method, the while loop continues going around and does not terminate:
Not Working Code:
private void print() {
boolean playerGuessedCorrectly = false;
while(!playerGuessedCorrectly) {
checkIfGuessIsMyNumber(playerGuessedCorrectly);
} // value of playerGuessedCorrectly is sent to method checkIfGuessIsMyNumber();
private boolean checkIfGuessIsMyNumber(boolean playerGuessedCorrectly) {
// userGuess is the next int entered by the user
// computerNumber is a random number between 1 and 100
if(userGuess == computerNumber) {
System.out.print("You have guessed my number.");
return playerGuessedCorrectly = true;
} else {
playerGuessedCorrectly = false;
}
}
To clarify, the while loop terminates when IF userGuess == computerNumber is inside the while loop, however, it does not work when it is split up into a different method and returns the value.
Note that when I print out the value given by the method, it has printed true. So I am confused to why it doesn't terminate when the value in the method is true, but it terminates with the if condition inside the while loop.

This line:
playerGuessedCorrectly = false;
and this line:
return playerGuessedCorrectly = true;
don't work because Java passes parameters by value. You're actually assigning false to the copy of playerGuessedCorrectly that is local to the checkIfGuessIsMyNumber function, not to the original version in the print function.
Following the not-working code line by line:
boolean playerGuessedCorrectly = false;
This works as intended.
while(!playerGuessedCorrectly) {
// ...
}
This would work as intended if the value of playerGuessedCorrectly was updated properly.
checkIfGuessIsMyNumber(playerGuessedCorrectly);
This is where you go wrong. This copies the value of the currently existing playerGuessedCorrectly variable into the parameter of the checkIfGuessIsMyNumber function. In that function, the new copy of playerGuessedCorrectly is reassigned different values, and you have a return statement, but note that you never actually use the return value of that function. If you instead did something like this:
playerGuessedCorrectly = checkIfGuessIsMyNumber(playerGuessedCorrectly);
your code would work as intended. You would also probably want to clean up the body of your checkIfGuessIsMyNumber function:
if(userGuess == computerNumber) {
System.out.print("You have guessed my number.");
return true;
} else {
return false;
}

Parameters in Java are passed by value. So when you call playerGuessedCorrectly = false, you just override the local copy of the playerGuessedCorrectly parameter.
A different approach to enclose your logic in a function is to have it return the appropriate value:
private void print() {
boolean playerGuessedCorrectly = false;
while(!playerGuessedCorrectly) {
playerGuessedCorrectly = checkIfGuessIsMyNumber();
}
}
private boolean checkIfGuessIsMyNumber() {
// userGuess is the next int entered by the user
// computerNumber is a random number between 1 and 100
if(userGuess == computerNumber) {
System.out.print("You have guessed my number.");
return true;
} else {
return false;
}
}

Related

java codebreaker - loop won't end

im trying to make a "codebreaker". User tries to guess random password (4 digit int)
program says if typed password is too low/high/equal. But im stuck, i cant figure out why my loop won't end.
public static void main(String[] args) {
int password = 1234;
startGame(checkNumber(loadNumber(),password));
}
public static void startGame(boolean isAWinner) {
int lives = 5;
do {
loadNumber();
lives--;
} while (lives > 0 || !isAWinner); //has lives or is not a winner
}
public static int loadNumber() {
System.out.println("Type the number");
Scanner scan = new Scanner(System.in);
int givenNumber = scan.nextInt();
return givenNumber;
}
//check if greater,lower or equal
public static boolean checkNumber(int number, int password) {
boolean isAWinner = false;
if (number == password) {
System.out.println("congratulations");
isAWinner = true;
}
if (number > password) {
System.out.println("too much");
}
if (number < password) {
System.out.println("too little");
}
return isAWinner;
}
You need to check your number that is loaded from user input while the game is running and assign the result to isAWinner.
Right now the while loop continues because isAWinner is never assigned a new value after an initial false value.
public static void startGame(boolean isAWinner) {
int lives = 5;
do {
//TODO: run checkNumber on result of loadNumber and assign its result to isAWinner
loadNumber();
lives--;
} while (lives > 0 || !isAWinner); //TODO: has lives AND is not a winner
}
In addition, you will want your loop to continue while lives is greater than zero AND the result is not a winner.
You need to do
do {
isAWinner = checkNumber(loadNumber(), password);
lives--;
} while (lives > 0 && !isAWinner);
Or else !isAWinner will always evaluate to true if it's not correct on the first guess. I also changed the OR to AND so that the loop will break once lives == 0 or isAWinner == True.
Also I would set your password variable outside the main method (as a field of your class), so you can access it inside your startGame method.
Your code forces the user to be correct on the first try. Go through it step by step (you can do this with a debugger and breakpoints, or just mentally with a problem this size).
You call startGame(checkNumber(loadNumber(), password));
The innermost function is called first, which in this case is loadNumber()
Let's assume the user enters an incorrect number (4321 for example)
The next innermost function is called next checkNumber(4321, 1234) which will print "too much" then return false
Now, your outer function is called like this startGame(false) since the checkNumber() function returned a false to it.
It gives you 5 lives, then calls loadNumber()
A new number is loaded, but a function call to check the number is never made!!
This happens until lives hits 0, which is when you expect it to terminate, but your while condition says this: Continue to play the game if the user has lives OR if the user has not won.
Since it isn't possible to win after the first iteration, the second part of the condition will never change after the first initial incorrect guess. You can solve the original question you asked by changing your code to while (lives > 0 && !isAWinner); but you will still have the issue of not having checked any of the answers after the first one.

Java Method Example clarification

I apologize if this question is uber-simplistic, but I'm still in the early stages of learning Java. I have an example program that calls other methods within the class, and I'm not totally following a few of the elements - hoping someone can clarify. It's a simply random number guessing game and it works fine, but I want to better understand some components. Specifically:
There is a boolean variable (validInput) that is declared but never appears to be used anywhere in the methods
There are 2 methods (askForAnotherRound and getGuess) with a 'while' loop that just has 'true' as the variable(?) - i.e. "while (true)."
This code is directly from the example in the book and, again, it works. I just want to better understand those 2 elements above. (I think the validInput variable is not useful as when I 'comment out' that line the code still executes). I'm curious, though, about the "while (true)" element. There is an option to set, in the askForAnotherRound, to set the return value to false (ending the program). Are boolean methods defaulted to 'true' when they are first executed/called?
Again...understand this is probably a super-simple question for most folks on here, but as a newb I just want to understand this as best I can...
Thanks!!!
// Replicates the number guessing game using 4 separate methods.
import java.util.Scanner;
public class GuessingGameMethod2
{
static Scanner sc = new Scanner(System.in);
public static void main(String[] args)
{
System.out.println("Let's play a guessing game!");
do
{
playARound();
}while (askForAnotherRound());
System.out.println("Thanks for playing!");
}
public static void playARound()
{
boolean validInput;
int number, guess;
String answer;
//Pick a Random Number
number = getRandomNumber();
//Get a guess
System.out.println("\nI'm thinking of a number between 1 and 10.");
System.out.print("What do you think it is? ");
guess = getGuess();
//Check the guess
if (guess == number)
System.out.println("You're right!");
else
System.out.println("You're wrong! The number was " + number + ".");
}
public static int getRandomNumber()
{
return (int)(Math.random() * 10) + 1;
}
public static int getGuess()
{
while (true)
{
int guess = sc.nextInt();
if (guess < 1 || guess > 10)
{
System.out.print("I said, between 1 and 10. Try again");
}
else
return guess;
}
}
public static boolean askForAnotherRound()
{
while (true)
{
String answer;
System.out.print("\nPlay again? Y or N: ");
answer = sc.next();
if (answer.equalsIgnoreCase("Y"))
return true;
else if (answer.equalsIgnoreCase("N"))
return false;
}
}
}
I don't see boolean validInput being used either. But if it were to be used somewhere it would probably be to check that you guess satisfies 1 <= guess <= 10.
When it comes to askForAnotherRound and getGuess here's what you should know:
while(true) is always executed. One way you can get out of the while loop is if you use the break statement or if the loop is in a function you can return something. The method askForAnotherRound() will always return either true or false. Depending on the returned value of askForAnotherRound() you will either play another round or not. Note that when you have
`do{
...
someActions()
...
}while(booleanValue)`
someActions() will be executed at least once before it checks for the value of booleanValue which, if it turns out false you'll exit out of the do/while loop. Boolean methods do not default to anything, you have to give them a value.
Hope this helps! I'm also in the process of learning Java right now, so good luck!
As I see you're absolutely true about validInput - it isn't used. May be it will be used in the following chapters.
As for askForAnotherRound() - no, boolean methods don't evalute to true, by default. Even more, Java compiler throw an error if it find a method which does not return value and finish it execution in some cases.
while(true) - it's infinite loop, so it will be executed untill some instruction which interrupts loop, in general it's return statement.
askForAnotherRound() do the following:
- asks user if he/she want to play again
- returns true if user input "Y"
- returns false if user input "Y"
- asks again in all other cases(so it doesn't finish execution) and etc.
Hope it'll help
The validInput is indeed worthless.
The infinite loops are required to read from the console to get a valid input. e.g
while (true)
//start infinite loop
{
int guess = sc.nextInt();
if (guess < 1 || guess > 10)
{
//continue the loop the input is not between 1-10
System.out.print("I said, between 1 and 10. Try again");
}
else
//break out of infinite loop, valid int
return guess;
}
If we take this method without the infinite loop, and i recommend trying this, it will simply return the value read even if it was not valid.
For example.
return sc.nextInt();
will allow any int returned, if we returned anything outside of the bounds in the current impl it would loop again until you enter a value between 1-10
The same is also true for ask for next round, its looping until a valid input is given.
I would bet the next exercises will use the validInput var as both these methods loop until a valid input is given.
You are right about validInput. It is not used. And probably missed after some code change. Should be removed.
while(true) - true is not variable but a boolean constant. It will basically make program run for ever in this case unless somebody kills program. Another alternative would have been to use break to exit out of loop on some condition.

Why wont this value work in a while loop?

This is essentially what i'm doing
while(true) //cant check for zero, there is more to do here
{
int val = 0;
for(SomeObject i : arrayListOfSomeObjects)
{
if(canDoSomething)
{
val++
}
}
if(val == 0)
{
break;
}
else
{
//do stuff with that nonzero value
}
}
It is strange, because the loop wont yield. As in, stop if unsuccessful. I have a proceedure that will keep going until it finishes, if it does not do anything (or does not meet my conditions in that operation) it wont change the value. Meaning that check on "val" should break the loop. But it doesn't. It just going. I have even printed "val" to the console, yet it returns 0.
At the end of that operation, even if the value IS 0, it keeps going!
Your code is correct.. Try the following..
int val = 1;
while(val>0)
{
//Some operation that will change the value IF successful
}
first int val = 0 supposed to be outside the loop because this will cause the value of the variable val always zero
You might need to make sure you are targeting the 'right' loop by adding a label:
label: while(true) //cant check for zero, there is more to do here
{
// ...
if(val == 0)
{
break label;
}
// ...
}
This will ensure that you break out of the while loop in case the check is in another loop or a switch statement. Otherwise, I am sure that something is causing the problem that you didn't show in your example. Beware of unwanted side-effects.

Break statement always gets executed

This might be a stupid question but we're beginners and I didn't find an answer to my problem so here it is: We're developping a file system (small based) and we have this method that is supposed to move files from one Directory to another. (Deleting the file or directory from one and adding to another.)
We're using ArrayLists to store the Items (Item is then superclass of Directory and File).
Because of the fact that everything has to be sorted alphabetically, the method to move contains a while loop to verify where the item has to be placed (no preferences to Directories or Files) but for some reason the break statement I inserted is ALWAYS executed (or at least that's what I think is the reason.) Thanks!
Here's the code:
if(item != null){
boolean bool = false;
int i = 0;
loop: while(!bool && i <= items.size()-1) {
if(i==0) {
if(checkIfAlphabetic(item.getName(), items.get(0).getName())){ items.add(0,item);
bool = true;
}
else{
break loop;
}
}
else if(checkIfAlphabetic(items.get(i-1).getName(), item.getName()) && checkIfAlphabetic(item.getName(), items.get(i).getName() )) {
items.add(i, item);
bool = true;
}
else i++;
}
if(!bool){
items.add(item);
}
setModificationTime();
}
I already excuse myself if there are some things unclear.
PS. Also for some reason the Item I want to add always gets added twice.
As requested, the code for checkIfAlphabetic:
private boolean checkIfAlphabetic(String search, String target){
int[] searchInt = search.codePoints().toArray();
int[] targetInt = target.codePoints().toArray();
int i = 0;
while(i<search.length() && i<target.length()){
if(searchInt[i] > targetInt[i]){
return false;
}
else if(searchInt[i] < targetInt[i]) return true;
else i++;
}
if(search.length() < target.length()){
return true;
}
else return false;
}
Your while loop is faulty. It will always stop after the first iteration, no matter what.
This is what happens in order of statements. This is pseudo-code, not Java. Don't copy/paste, it won't work.
boolean bool = false;
int i = 0;
// entering the while loop:
if (!bool && i <= items.size() - 1) // returns true. We go in the while loop.
if (i == 0) // returns true, we go in that block.
if (check... ) // if this returns true, this happens:
bool = true;
else // if the previous statement returns false, this happens:
break;
So here, if the check... returns false, we're gonna get out of the loop. Let's continue in the other case:
// nothing else happens inside the loop, so go back to the loop condition.
if (!bool && i <= items.size() - 1) // Hey, wait a minute, bool is true. So "not" true is false. The condition is therefore not met, let's leave the loop.
So this is what happens, after a single execution, no matter what, your code exits the loop. In your scenario, bool = true is the near absolute equivalent to a break.
This is what you need to fix.
If I had to write your code, this is how I'd do it:
List<Item> items = ... ;
java.util.Collections.sort(items, new ItemNameComparator());
private static class ItemNameComparator implements Comparator<Item> {
#Override
public int compare(Item a, Item b) {
return a.getName().compareTo(b.getName());
}
}
If you use Java 8:
List<Item> items = ...;
items.sort((a, b) -> a.getName().compareTo(b.getName()));
All the tools exist in the Java libraries, use them instead of reimplementing them again and again.

WHILE repeats before ELSE is evaluated

I've nested an IF ELSE statement inside a WHILE statement, but am confused as to why the WHILE is interpreted before the ELSE (when the IF fails). A user is asked to enter a number from 1-10 (inclusive). If the number is inside that range, the program ends. If it's outside of that range, I want to display an error and then prompt them to again enter a number.
It works well if I put the "prompt" before the WHILE, but then I have to put it again inside the ELSE statement for it to show up again. I found this question, but it didn't appear to answer the issue I'm running into. I'm admittedly a Java novice, so I apologize if I'm missing some fundamental aspect of Java.
import java.util.Scanner;
public class Rangecheck {
private static int userNumber; //Number input by user
private static boolean numberOK = false; //Final check if number is valid
//String that will be reused in the DO statement
private static String enterNumber = "Please enter a number from 1 to 10: ";
public static void main(String[] args) {
//Print string
while(!numberOK) // Repeat until the number is OK
{ System.out.println(enterNumber);
Scanner input_UserNumber = new Scanner(System.in); //input integer
userNumber = input_UserNumber.nextInt();
if (10>= userNumber && userNumber >= 1) //Check if 10>=input>=1
{
/*
** If number was valid, congratulate the user and mark numberOK true
*/
System.out.println("Good job! The number you entered is "+userNumber+".");
numberOK = true; // Congratulate user / exit loop if successful
}
else ; //if (10 < userNumber && userNumber < 1)
{
System.err.println("The number entered was not between 1 and 10!");
System.err.print(enterNumber); // Error; user retries until successful
}
}
}
}
I'd expect the System.err.println() to be evaluated in the else statement and then the whileto be evaluated, so that this gets returned:
The number entered was not between 1 and 10!
Please enter a number between 1 and 10:
I've sort of worked around this by putting enterNumber just before while, then putting a second
println in the else statement immediately following the error. It returns what I expect, but I believe I'm fundamentally misunderstanding something.
Let's suppose you have the following code:
while (whileCondition) {
//Inside while, before if
if (ifCondition) {
//Inside if
} else {
//Inside else
}
}
This cycle will execute repeatedly, until the whileCondition becomes false. Whenever the ifCondition is true, the operations inside if will be executed, otherwise the operations inside else will be executed.
Back to your problem:
Your line of
System.out.println(enterNumber);
is at the start of the while. So before the code even gets to your if, the content of enterNumber is displayed on the console. Later, your if is evaluated and if you enter, let's say 22, the condition given to the if will be false and the content of the else block will be evaluated.
The else statement is repeated before the while statement. There can however sometimes be a problem with Streams. A Stream does not necessarily print the data immediately. Especially in the case one uses two different streams, the order of printing the output data can be interleaved.
You can use:
System.err.flush();
to ensure the data is written to the console first.
Add this statement in else body:
numberOk=false;

Categories

Resources