while loop conditions java - java

String userGuessParameters = "a,b";
while (!Character.isDigit(userGuessParameters.charAt(0)) ||
!Character.isDigit(userGuessParameters.charAt(2)) ||
userGuessParameters.length() != 3 ||
(int)(userGuessParameters.charAt(0)) >= parameters[0] ||
(int)(userGuessParameters.charAt(2)) >= parameters[1]) {
System.out.print("Mida kaevame (rida, veerg): ");
userGuessParameters = userInput.nextLine();
userGuessParameters = userGuessParameters.replaceAll(" ", "");
}
I'm trying to check if all the required conditions are fulfilled in the while loop. parameters is what user entered in array form. Let's assume parameters = [4, 4] (Parameters is used to create a 4x4 map).
I need userGuessParameters to be:
Numbers
Length equal to 3 (the input looks like this: 2,2 when the spaces are removed)
Smaller than the biggest coordinate (which is 3,3)
But for some reason, the loop never exits. I'm almost certain it is because of the last two conditions in the while loop, but I can't find the mistake.

You are using the logical OR operator when you should be using AND (you want all the conditions to be met, not just at least one of them).
Use the java notation for AND which is &&:
(!Character.isDigit(userGuessParameters.charAt(0)) &&
!Character.isDigit(userGuessParameters.charAt(2)) &&
userGuessParameters.length() != 3 &&
(int)(userGuessParameters.charAt(0)) >= parameters[0] &&
(int)(userGuessParameters.charAt(2)) >= parameters[1])

Try to use && instead of || like this:
while (!Character.isDigit(userGuessParameters.charAt(0)) && !Character.isDigit(userGuessParameters.charAt(2))
&& userGuessParameters.length() != 3 && (int)(userGuessParameters.charAt(0)) >= parameters[0]
&& (int)(userGuessParameters.charAt(2)) >= parameters[1]) {

Related

Can you connect multiple "not equals to" conditions together in one condition in java?

For instance, would this program run:
*loop code.......*
if (variable1 != variable2 != variable3 != variable4){break loop;}
What you're really trying to test is whether there are any duplicate values in your set of variables. Here's a simple method to test that:
static boolean allUnique(Object... values) {
return new HashSet<>(Arrays.asList(values)).size() == values.length;
}
You can call it like this:
if (allUnique(variable1, variable2, variable3, variable4)) break loop;
if (new HashSet(variable1, variable2, variable3, variable4).size == 4) ...
Yes, you need to use the logical && AND operator.
More info in this SO answer.
Essentially you will use something like:
if (variable1 != variable2 && variable1 != variable3 && variable1 != variable4)
This will check that variable 1 is not equal to 2, 3 or 4.

Check whether List<Integer> is equal to an int

This is some of my current code:
Class usedCoords contains:
public List<Integer> USEDX = new ArrayList<Integer>();
main function contains:
if(fX % gridX == 0 && fZ % gridZ == 0 && ALPHA != 0 && usedcoords.USEDX == fX) { }
Note I also done: usedCoords usedcoords = new usedCoords();, that is why I named it usedcoords.
My main task is that I want to make the usedcoords.USEDX == fX possible. Currently I will get an error because fX is an integer. USEDX has integers as well so how do check that the any integer in USEDX is equal to fX?
Thanks in advance.
Use List#contains() -- and it's more readable and conventional not to have variable names start with an uppercase unless they are constants:
if (fX % gridX == 0 && fZ % gridZ == 0 && alpha != 0 && usedcoords.usedX.contains(fX)) {
...
}
The int variable fX will be automatically boxed into the Integer type by the compiler.
USEDX has integers as well so how do check that the any integer in USEDX is equal to fX?
By calling List.contains(Object) which returns true if this list contains the specified element. Something like,
if (USEDX.contains(fx)) {
// ...
}
For efficiency-sake, you may use Hashtable<Integer> instead of List because it requires O(1) (constant) time to search for a value.

Java and and or operator not working

I am using the following statement to break out of the loop when two conditions met:
while (true)
{
if (uAnswer1.equals(answerB1) || uAnswer1.equals(answerB2)
|| uAnswer1.equals(answerB3)|| uAnswer1.equals(answerB4)
&&
uAnswer2.equals(answerS1)|| uAnswer2.equals(answerS2)){
break;
}
The loops breaks when one or both && conditions are met. However, I wrote the code to break the loop ONLY when both conditions are true.
Is there something missing from above statement?
Regards,
Shei7141.
wrap them in parentheses
if ( (uAnswer1.equals(answerB1) || uAnswer1.equals(answerB2)
|| uAnswer1.equals(answerB3)|| uAnswer1.equals(answerB4))
&&
(uAnswer2.equals(answerS1)|| uAnswer2.equals(answerS2)) )
or
even make a HashSet of correct answers and do this will be clean and will be efficient too
answers1Set.contains(uAnswer1) && answers2Set.contains(uAnswer2)
above code shows that uAnswer1.equals(answerB4) && uAnswer2.equals(answerS1) are in AND condition
while (true)
{
if ((uAnswer1.equals(answerB1) || uAnswer1.equals(answerB2)
|| uAnswer1.equals(answerB3)|| uAnswer1.equals(answerB4))
&&
(uAnswer2.equals(answerS1)|| uAnswer2.equals(answerS2))){
break;
}
while (true)
{
if ((uAnswer1.equals(answerB1) || uAnswer1.equals(answerB2)
|| uAnswer1.equals(answerB3)|| uAnswer1.equals(answerB4))
&&
(uAnswer2.equals(answerS1)|| uAnswer2.equals(answerS2)))
break;
}

(Code Review) Java if statement involving logical and(&&) operator [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have started programming a few weeks ago in java/android. I want to write a small tic tac toe game as an android app but I'm having trouble with my method that will check for the winner. It is as follows:
public void checkForWinner() {
if( taken[0] && taken[3] && taken[6] ||
taken[0] && taken[1] && taken[2] ||
taken[2] && taken[5] && taken[8] ||
taken[6] && taken[7] && taken[8] ||
taken[0] && taken[4] && taken[8] ||
taken[2] && taken[4] && taken[6] ||
taken[1] && taken[4] && taken[7] ||
taken[3] && taken[4] && taken[5] == 1 ){}
}
Here I have an array called taken that holds 9 integers, each of those integers being either a one, meaning player one owns that block, or a two, meaning player two ows that block. Current, I am trying trying all possible scenarios in which player one would be the winner but eclipse is telling me that The operator && is undefined for the argument type(s) int, int. The error only seems to be showing for the first logical and operation of each line of the if statement. For example the first error goes up to taken[0] && taken[3] and then disappears until the next line.
Alternatively, you can check taken[n] values to see if they hold 1 or 2, if you think that'd make your code clearer:
(taken[0]==1 && taken[3]==1 && taken[6]==1)
Keep in mind that the && operator expects boolean operands...so it won't work with your int array the way you're expecting it to.
Swap your && to ==, you're trying to see if they're all the same value I assume which would show a winner, and be sure to use parentheses to sort it out, so one win condition would look like
((taken[0] == taken[3]) && (taken[0] == taken[6]))
However, this will only tell you that some player won, not which player. I guess you could check to see which player made the last move once it is determined that some one has won and declare that player as the winner.
In java,
if (a && b) or if (a || b)
works only if a and b are booleans / boolean expressions.
Here I have an array called taken that holds 9 integers, each of those integers being either a one, meaning player one owns that block, or a two, meaning player two owns that block.
In that case, you can replace the logical operators with bitwise operators:
int winner = taken[0] & taken[3] & taken[6]
| taken[0] & taken[1] & taken[2]
| taken[2] & taken[5] & taken[8]
| taken[6] & taken[7] & taken[8]
| taken[0] & taken[4] & taken[8]
| taken[2] & taken[4] & taken[6]
| taken[1] & taken[4] & taken[7]
| taken[3] & taken[4] & taken[5];
Then the variable winner will contain 1 if player 1 won, 2 if player 2 won, 0 if neither of them won, or 3 of both of them won (which probably isn't possible in your game).
1) You cant use && or || operators on int variables because the are meant for boolean values.
2) also use brackets to group conditions like
if (
(taken[0]==1 && taken[3]==1 && taken[6]==1)||
(taken[0]==1 && taken[1]==1 && taken[2]==1)||
...
OK there are two main problems here.
First, taken[0] needs to return either true or false to be able to remain as-is within the if-statement. You have mentioned it is an integer, so for it to return true or false, you'll need to do a comparison, such as taken[0] == 1.
Second, you need to use parentheses and do some groupings. Java does not respect whitespace. Instead of if( taken[0] && taken[3] && taken[6] || ..., you'll need to do if( (taken[0] && taken[3] && taken[6]) || .... That is, you need to put parentheses around each set of groupings.
The reason Eclipse is erroring is because the && operator is only for comparing booleans. That is true && true. You're giving it integers and it doesn't like that.
One way you might want to solve this is to write a function that determines if a solution is reached. So maybe something like:
private boolean isSolutionPresent(int[] taken, int index1, int index2, int index3) {
return (taken[index1] == 1 ) && (taken[index2] == 1) && (taken[index3] == 1);
}
Then you could convert your if statement to something like:
if (isSolutionPresent(taken, 0, 3, 6) ||
isSolutionPresent(taken, 0, 1, 2) ||
isSolutionPresent(taken, 2, 5, 8) || //... etc, removing the final ==1
Since the isSolutionPresent method returns a boolean (that is, true/false) you can apply the || operator to it.

Java: do while wont while

Anfallare.armees = 10;
Forsvarare.armees = 10;
do{
Anfallare.slaTarningar();
Forsvarare.slaTarningar();
Anfallare.visaTarningar();
Forsvarare.visaTarningar();
MotVarandra(Anfallare, Forsvarare);
status(Anfallare, Forsvarare);
}while(Anfallare.armees == 0 || Forsvarare.armees == 0);
is what I have, I tried also a
System.out.println(Anfallare.armees + " :: " + Forsvarare.armees);
after this do while and it is 9 and 10, neither of them are 0.
Why does it run this once?
Because, as you point out, after the first run, Anfallare.armees is 9, which does not equal 0, and Forsvarare.armees is 10, which is not equal to 0.
Perhaps you meant
while(Anfallare.armees != 0 && Forsvarare.armees != 0)
while(Anfallare.armees == 0 || Forsvarare.armees == 0)
Means continue only if Anfallare.armees == 0 || Forsvarare.armees == 0. In your example, neither is 0 so it will not loop.
while(Anfallare.armees != 0 && Forsvarare.armees != 0)
Means, stop looping if either are zero (Anfallare.armees != 0 && Forsvarare.armees != 0)
You may actually be wanting:
while (Anfallare.armees != 0 || Forsvarare.armees != 0)
means, stop looping if both are 0
You need to do
while(Anfallare.armees != 0 || Forsvarare.armees != 0);
To make it loop until both of those variables are 0
Because you start as do{}, which forces to execute at least once the code, and then you have while x==0, which is not true. Since it is not true, the loop ends.
As you see in the other answers, you may need to check for x != 0 (not equal to), or even better x>0 (in the event x could take a negative number)
Take some time to read the Oracle's Java tutorials I linked in the other answer to you. It explains the loop logic very well.
If I understand you correctly, your do-while loop body is executed once, but you think it shouldn't have run at all - is this correct?
The reason is, in do-while the loop condition is checked only after the loop body is executed. I.e. it will always run at least once.
It's hard to tell without the complete code, but it may be that you just need to invert the condition:
}while(!(Anfallare.armees == 0 || Forsvarare.armees == 0));
You condition says ... as long as one of them is 0. What you'll want is this:
while(Anfallare.armees > 0 || Forsvarare.armees > 0);
the reason is simple
while loops when the expression is true.
so what you want is
while(Anfallare.armees != 0 || Forsvarare.armees != 0);
but even better I would suggest
while(Anfallare.armees > 0 || Forsvarare.armees > 0);
this would allow you to be sure that your armee count never gets negative ^^
Jason

Categories

Resources