I may just be tired and not thinking properly anymore, but why is "13" only printed once here? (intelliJ tells me that "i == 11 | i == 13" is always true but I don't see how that makes sense)
for (int i = 0; i < 14; i++) {
System.out.println(i);
String line = clientReader.readLine();
int length = line.length();
if (i == 0 || i == 5 || i == 6) {
line = line.substring(7, length - 6);
} else if (i == 1 || i == 2 || i == 3 || i == 4 || i == 8 || i == 9 || i == 10 || i == 12) {
line = line.substring(8, length - 7);
} else if (i == 7) {
line = line.substring(9, length - 8);
} else if (i == 11 || i == 13) {
line = line.substring(10, length - 9);
}
data[i] = line;
System.out.println(i);
}
p.s. The line.substring does not give an error, if I add System.out.println(line) at the end of the last else if it prints the correct thing.
The last else if is always true because your loop control variable runs from 0 until 13 and the only two numbers you haven't checked before the last else if is 11 and 13 therefore if none of the above conditions are true then i will either be 11 or 13 hence why IntelliJ is smart enough to know it's always true and hence control will always be bound inside the last else if block when the above conditions are not met.
If you increase the loop condition to something like i < 15 or above then IntelliJ wouldn't state else if (i == 11 || i == 13) is always true as i could be 14.
Related
i am trying to detect a jokerStraightFlush when analysing a poker hand.
I need to add a feature where this hand works 8S JK 6S JK 4S. The JK are jokers. I am using the exact same code logic as https://www.codeproject.com/Articles/38821/Make-a-poker-hand-evalutator-in-Java.
cardsTable represents the distribution of the Card ranks present in the hand. Each element of this array represents the amount of card of that rank(from 1 to 13, 1 being ace) present in the hand. So for 8S JK 6S JK 4S, the distribution would be
0 0 0 0 1 0 1 0 1 0 0 0 0 0
note that the position 1 is for ace (because it's simpler)
I need to find a way to detect if cardsTable[i] == 1 failed and decrement the amount of jokers used (numberOfJokers) to detect a jokerStraightFlush because in this incomplete piece of code, numberOfJokers dont decrement and i dont know how to write it in a nice way. What i do here is i check if the card at this rank exists cardsTable[i] == 1 or if its a joker... but i dont know how to check for a joker in the others consecutive rankings
I dont know if i'm clear, it's a twisted situation.. if i'm not let me know.
straight = false; // assume no straight
int numberOfJokers = 2; //(the maximum number of jokers in a hand is 2)
for (int i = 1; i <= 9; i++) { // can't have straight with lowest value of more than 10
numberOfJokers = 2 //reset the number of jokers available after each loop
if ((cardsTable[i] == 1 || numberOfJokers > 0) &&
(cardsTable[i + 1] == 1 || numberOfJokers > 0) &&
(cardsTable[i + 2] == 1 || numberOfJokers > 0) &&
(cardsTable[i + 3] == 1 || numberOfJokers > 0) &&
(cardsTable[i + 4] == 1 || numberOfJokers > 0)) {
straight = true;
break;
}
}
I also have this code but i don't know how to detect if the first condition failed so i can decrement the number of jokers remaining.
for (int i = 1; i <= 9; i++) {
numberOfJokers = 2
if (cardsTable[i] == 1 || numberOfJokers>0) {
if (cardsTable[i + 1] == 1 || numberOfJokers>0) {
if (cardsTable[i + 2] == 1 || numberOfJokers > 0) {
if (cardsTable[i + 3] == 1 || numberOfJokers > 0) {
if (cardsTable[i + 4] == 1 || numberOfJokers > 0) {
straight = true;
break;
}
}
}
}
}
}
Due to "short-circuiting" behaviour, you don't need to detect the left operand of a || resulted in true: the right operand gets evaluated if the left one was false, only.
(cardsTable[i + c] == 1 || numberOfJokers-- > 0) would work; note that
(numberOfJokers-- > 0 || cardsTable[i + c] == 1) would not.
Whether or not such code is maintainable or readable is an independent consideration, as is the quality of the overall approach to problem and solution.
Conceptually, you just need 3 of the 5 slots to be equal to 1, and the other 2 equal to 0. You could do something like this:
for (int i = 1; i <= 9; i++) {
boolean straight = true;
int numberOfJokers = 2;
for (int j = i; j <= i + 5; j++) { // I used a for loop instead of writing the statement 5 times
if (cardsTable[j] > 1) { // Can't have straight if more than one of a kind
straight = false;
}
if (cardsTable[j] == 0) {
numberOfJokers--;
}
}
if (numberOfJokers >= 0 && straight == true) {
break;
}
}
Alternatively, maybe a simpler way would be to add a position in the cardsTable array indicating the number of jokers.
nvm i used a function inside my conditions.. i don't know if it's the right way but it's logic to me x)
straight = false; // assume no straight
for (int i = 1; i <= 9; i++) // can't have straight with lowest value of more than 10
{
remainingJokers=jokers;
System.out.println("resetjokers : "+jokers);
if (cardsTable[i] == 1 || useJoker()) {
if (cardsTable[i + 1] == 1 || useJoker()) {
if (cardsTable[i + 2] == 1 || useJoker()) {
if (cardsTable[i + 3] == 1 || useJoker()) {
if (cardsTable[i + 4] == 1 || useJoker()) {
System.out.println("straight");
straight = true;
topStraightValue = i + 4; // 4 above bottom value
break;
}
}
}
}
}
}
}
private boolean useJoker() {
int remaining = remainingJokers;//remainingJokers is a global variable
remainingJokers--;
return remaining>0;
}
So i have a string in military time format : "1532" corresponding to 3:32pm.
I'm trying to write a method to check if each digit in time string is an appropriate digit. So the first element cannot be greater than 2 or equal to 0, and so forth. Currently, my code doesn't run past the second log statement and I'm hoping you guys could help!
cheers!
String mOpen = "1532";
Log.d("hoursTesting","pass1, length is > 2");
if(mOpen.getText().length() == 4)
{
Log.d("hoursTesting","pass2, length is == 4");
char[] tempString = mOpen.getText().toString().toCharArray();
if(tempString[0] != 0 && tempString[0] < 3)
{
Log.d("hoursTesting","pass3, first index is != 0 and < 3");
if(tempString[0] == 1)
{
Log.d("hoursTesting","pass4, first index is 1");
if(tempString[2] <= 5)
{
Log.d("hoursTesting","pass5, third index is <= 5, success!");
}
}
else //tempString[0] is equal to 2
{
Log.d("hoursTesting","pass4, first index is 2");
if(tempString[1] < 4)
{
Log.d("hoursTesting","pass5, second index is <3");
if(tempString[2] <= 5)
{
Log.d("hoursTesting","pass6, third index is <= 5, success!");
}
}
}
}
}
tempString contains characters, not numbers.
i.e. '0' not 0 etc.
Easiest fix is to compare characters e.g. tempString[0] == '1' Alternatively, you can do something like int digit1 = tempString[0] - '0'; - but that kind of assumes you already know you just have digits in the string.
Note that cos of those clever ASCII guys and their tricky character set '0' < '1' < '2' etc, so you can still say if (str[0] < '2') etc. You just need to be a bit careful that you are only dealing with digits.
Personally I'd convert the first 2 chars to a number and the second 2 chars to a number and then just check 0 <= number1 <= 23 and 0 <= number2 <= 59.
You are comparing char with int here:
if(tempString[0] != 0 && tempString[0] < 3)
It should work like this:
if(tempString[0] != '0' && tempString[0] < '3')
I would substring the hours and minutes components and then check to see if each one be in range:
public boolean isTimeValid(String mOpen) {
int hours = Integer.parseInt(mOpen.substring(0, 2));
int minutes = Integer.parseInt(mOpen.substring(2));
if ((hours >= 0 && hours <= 24) && (minutes >= 0 && minutes <= 59)) {
return true;
}
else {
return false;
}
}
I don't understand why the following class prints out:
true
false
I thought the output should be:
false
false
because this line prints false:
System.out.println((11 >= 1 || 11 <= 10) & (true == false));
so this line should also print false:
System.out.println(in1To10(11, false));
What am I missing here? Here's the class.
public class TestClass {
public static void main(String[] args) {
System.out.println(in1To10(11, false));
System.out.println((11 >= 1 || 11 <= 10) & (true == false));
}
public static boolean in1To10(int n, boolean outsideMode) {
if ((n >= 1 || n <= 10) & (outsideMode == false)) {
return true;
}
return false;
}
}
You want to test if value is in the range 1 to 10 ?
If thats the case, change (n >= 1 || n <= 10) to (n >= 1 && n <= 10)
( true )
( true ) & ( true )
(true || false ) & ( true ) <--- false == false is true!
if ((n >= 1 || n <= 10) & (outsideMode == false)) {
return true;
}
Look, is n >= 1? Yes, so it's true. true v p <-> true, so Java doesn't even check further. true /\ true <-> true so we enter if. return true;
Your code is in plain English:
if ((n is greater or equal to 1 OR smaller or equal 10) AND is the mode set to false)
return true
If you want it to return true if the number is between 1 and 10 incl, it would be:
if ((n is greater or equal to 1 AND smaller or equal 10) AND is the mode set to false)
return true
which in Java is:
if ((n >= 1 && n <= 10) && (outsideMode == false)) {
return true;
}
Also remember to use && and || as they are logical operators instead of | and & bitwise logical operators when dealing with boolean values.
so this line should also print false: System.out.println(in1To10(11, false));
No, this line should not print false. Although the first parameter, 11, indeed turns the expression n >= 1 || n <= 10 from your method into 11 >= 1 || 11 <= 10, which matches your other expression, the second parameter, false, turns outsideMode == false into false == false, while your other expression has true == false.
That is why the two outputs are different: the output from in1To10 is true because the comparison false == false produces true, while the output from main is false, because true == false produces false.
Note: your expression does not match its stated goal of checking if n is between 1 and 10, inclusive. You need to replace || with && to accomplish that.
I think you missed that in function in1to10, you are comparing (false == false) and in main you are comparing (true == false). And so is the result. Hope this helps.
Let's decompose the test in in1to10 :
Parameters : outsideMode = false; n = 11;
(n >= 1 || n <= 10) :=> (11 >= 1 || 11 <= 10) :=> (true || false) :=> so TRUE
outsideMode == false :=> false == false :=> so TRUE
In the end :
public static boolean in1To10(int n, boolean outsideMode) {
if ((TRUE) & (TRUE)) {
return true;
}
return false;
}
:=> return TRUE !
This will return true. How?
if ((n >= 1 || n <= 10) & (outsideMode == false)) {
return true;
}
You are passing 11 and false to the function.
When it goes to inside the first condition it will check that n >= 1 mean 11 >= 1 that is true, so it will not check n<=10. Again it will check the second condition and your outsideMode is false,
That will be like this, (true) & (true)
hence the whole condition will be true and the funciton will return true.
Again the second condition,
(11 >= 1 || 11 <= 10) & (true == false)
It will return false. How?
As 11 >= 1 is true and again it will not check 11 <= 10, and right side is false.
So the condition will be become like this,
true & false that will be false.
I am having trouble finishing the straight method for a poker hand. I don't understand why my code doesn't work.
public static boolean containsStraight(int [] hand)
{
boolean straight = false;
for(int i = 0; i < 5; i++)
{
if (hand[i] == 2 && hand[i] == 3 && hand[i] == 4 && hand[i] == 5 && hand[i] == 6)
{
straight = true;
}
if (hand[i] == 3 && hand[i] == 4 && hand[i] == 5 && hand[i] == 6 && hand[i] == 7)
{
straight = true;
}
if (hand[i] == 4 && hand[i] == 5 && hand[i] == 6 && hand[i] == 7 && hand[i] == 8)
{
straight = true;
}
if (hand[i] == 5 && hand[i] == 6 && hand[i] == 7 && hand[i] == 8 && hand[i] == 9)
{
straight = true;
}
}
return straight;
}
As pL4Gu33 has already stated in his answer, your comparison is faulty. Essentially, each step through the for-loop leaves hand[i] at a constant value (say, 4). That means that your if-statements are checking:
if(4 == 4 && 4 == 5 && 4 == 6 && 4 == 7 && 4 == 8) {
...
}
This will never evaluate to true. If you knew for certain that you had five elements in the hand and that the hand was already sorted, you could do
if (hand[0] == 2 && hand[1] == 3 && hand[2] == 4 && hand[3] == 5 && hand[4] == 6) {
...
}
However, I'm going to show you a better answer.
The first thing you should do is to sort your hand. Once you do that, it's easy to step through the hand and check to see if the next card in the hand is exactly one greater than the previous card. If you get to the end and this holds true, then it's a straight.
/*
* You will need to import java.util.Arrays
*/
public boolean isStraight(int[] hand) {
if(hand == null || hand.length != 5) {
return false;
}
else {
// Automatically sort the hand
Arrays.sort(hand);
// Set the "previous" variable to a theoretically impossible value
int prev = -1;
// Iterate through the hand and see if the next card is exactly one more than
// the previous one.
for(int i = 0; i < hand.length; i++) {
// If prev is -1, then this is the first time through the for-loop
// If the card that we're on has a value of the previous card + 1,
// we still have the possibility of a straight.
if(prev == -1 || (prev + 1) == hand[i]) {
prev = hand[i];
}
else {
return false;
}
}
return true;
}
}
You say in every iteration hand[i] must be 2 AND 3 AND 4 AND 5. That is impossible. There is only one number in hand[i].
The problem is, that you are using the cycle incorrectly, because you are always checking the value of the same card in hand[i]. My suggestion would be either to do a sort first, or if you want to be more efficient, you can use a second field of booleans, that would indicate, whether a card of given value is present in your hand. This way you can easily check, if you have any number of cards in succesion.
public static boolean containsStraight(int[] cards) {
int count = 0;
boolean[] valueInHand = new boolean[10];
for (int card : cards) {
valueInHand[card] = true;
}
for (boolean value : valueInHand) {
if (value == true) {
count++;
} else {
count = 0;
}
// works for any number of cards
if (count == cards.length) {
return true;
}
}
return false;
}
What am I doing wrong here?
I am wanting to display integers from 1-100 who are divisible by either 6 or 7. That's done and working. The next step is to not display any that are divisible by both...that isn't working in my loop (those integers are still displaying)
for (int i = 1; i < 100; i++)
if (i % 6 == 0 || i % 7 == 0 && i % (6 * 7) != 0){
println(i);
}
Thanks!
Joel
try making your condition more explicit by adding (...), like so:
if (((i % 6 == 0 || i % 7 == 0) && (i % (6 * 7) != 0)) {
}
by default && takes precedence over ||
Missing parentheses:
for (int i = 1; i < 100; i++) {
if ((i % 6 == 0 || i % 7 == 0) && i % (6 * 7) != 0) {
println(i);
}
}
You could also use the exclusive or
for (int i = 1; i < 100; i++) {
if ((i % 6 == 0) ^ (i % 7 == 0)) {
println(i);
}
}
or just an unequal if ((i % 6 == 0) != (i % 7 == 0))
Since exclusive or is not used that often, I doubt this will increase the readability of the code...
I would simply stop worrying about how to evaluate precedence, and use something like:
for (int i = 1; i <= 100; i++) {
if ((i % 42) == 0) continue;
if ((i % 6) == 0) println (i);
if ((i % 7) == 0) println (i);
}
I'm assuming here that 1-100 was an inclusive range in which case you should use <= rather than <. It won't matter for your specific case since 100 is divisible by neither 6 nor 7.
Guess what? Any decent optimising compiler (including JIT ones) will probably end up generating the same code as for all the other possibilities. And, even if it didn't, it wouldn't matter unless you were calling this function a great many times.
I think that's a tiny bit more readable than:
if (i % 6 == 0 || i % 7 == 0 && i % (6 * 7) != 0) ...
or, worse yet, the Lisp-like thing you'll have to turn it into to get it working properly :-)
Keep in mind one possibility - you can change your loop to make it more efficient (sevenfold), for the specific case with 6 and 7, thus:
for (int i = 7; i <= 100; i += 7)
if ((i % 6) != 0)
println (i);
This uses the for loop itself to only check multiples of 7 and print them if they're not also multiples of 6.
for (int i = 1; i < 100; i++)
if ((i % 6 == 0 || i % 7 == 0) && !(i % 6 == 0 && i % 7 == 0)){
println(i);
}