What am i missing in this java code? - java

if (array[i]<(char)65 || array[i]>(char)122 &&
array[i]>(char)91 || array[i]<=(char)96)
System.out.println("False")
in this code, when i try to assign character 'C' (which is 67 btw) to array[i] it still says false. I did the math and it's not supposed to print "false" as I stated below this line.
(67 < 65 = 0 || 67 > 122 = 0) = 0
(67 > 91 = 0 || 67 <= 96 = 1) = 1
So, this leaves us: 0 & 1 = 0 .
Any ideas?

With some formatting, your code is:
if ( array[i] < (char)65 || array[i] > (char)122 &&
array[i] > (char)91 || array[i] <= (char)96 )
System.out.println("False");
Since && has higher precedence than || (see Operators), this is equivalent to:
if ( array[i] < (char)65
|| ( array[i] > (char)122 && array[i] > (char)91 )
|| array[i] <= (char)96 )
System.out.println("False");
which, since && is short circuiting, is behaviorally equivalent to:
if ( array[i] < (char)65
|| array[i] > (char)122
|| array[i] <= (char)96 )
System.out.println("False");
which, since the last case covers the first, is logically equivalent to:
if ( array[i] > (char)122 || array[i] <= (char)96 )
System.out.println("False");
You'll print False whenever the value it greater than 122 or less than or equal to 96. 67 is less than 96, so you print False. As pointed out the in comments, there is a precedence to the operators. Rather than learning all the details (to predict cases like this), it's easier just to use enough parentheses.

The && is evaluated first, and then the || according to the Java Operator Precedence. So it gets evaluated the following way for 'C'
67>122 && 67>91 //false
67<65 || false //false
false || 67<=96 //true
if you uses parentheses it will solve this problem
if ((array[i]<(char)65 || array[i]>(char)122) &&
(array[i]>(char)91 || array[i]<=(char)96))

I think you may try this by using brackets ():
if ((array[i]<(char)65 || array[i]>(char)122) && (array[i]>(char)91 || array[i]<=(char)96))
System.out.println("False")
Also to note that && has higher precedence

Related

Condition for while loop doesn't work in java

I want the loop to repeat if the number is not equal to 0 and rest%2 is equal to 1 or -1. But this does not seem to work:
while (number != 0 && rest%2 == 1 || rest%2 == -1)
How do I have to write the code so it works?
While it's good to learn about operator precedence, your expression can be reduced:
while (number != 0 && rest%2 != 0)
Put another way, n % 2 is 0 for positive and negative even numbers and something not even must be odd (which is what you are testing).
This is the correct way:
while (number != 0 && (rest%2 == 1 || rest%2 == -1))
Have a look at Java operator precedence here https://introcs.cs.princeton.edu/java/11precedence/
If I understand correctly you want to enter the loop in two cases:
number != 0 && rest%2 == 1
OR
rest%2 == -1
If that's true consider using parentheses:
while ((number != 0 && rest%2 == 1) || rest%2 == -1)

get characters from string by index in java

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.

Java difference between || and && in this example

I am making a counter between number ranges and not sure the correct way to do this. I have always used the || operator but reading some examples, I feel I should be using the && command. Here is my example problem...
if(value >= 1 || value <=10){
count1++;
}
else if(value >= 11 || value <= 20){
count2++;
// AND SO ON........
Or should I be using the && operator like
if(value >= 1 && value <= 10){
count1++;
}
else if value >= 11 && value <= 20){
count2++;
}
|| means "or".
&& means "and".
value >= 1 || value <= 10 makes no sense because it's always true. All numbers are 1 or more, or 10 or less. Some numbers are both, but that doesn't matter.
value >= 1 && value <= 10 makes far more sense. There's a limited range of numbers ([1..10]) for which both the first condition and the second condition are true.
|| is the or operator, so the condition value >= 1 || value <=10 is true for all values if you think about it. So, unless you want your counts to be meaningless, use && which is the and operator.

operator precedence && and ||

please ignore the question - its wrong
I am not sure if my question is issue is related to operator precedence- Just to rule out that I added additional bracket. My understanding is in that case that code in each bracket will be executed. So basically all the OR operation will happen and its output would be AND'ed to condition a.
I have below set of parameters a = true and c = 254 , b is not availble ( b is initialized to 0 -At any given time either b or c only is availble) . So for the above condition I am expecting if condition to result in true but it's resulting in false condition. Any reason why ? Also what is best way to debug such things as in where exactly condition is going wrong - any pointers
if ((a == true) && ((b == 460) || (b == 454) || (b == 455) ||
(c> 13568 && c< 14335) ||
(c> 10640 && c< 10655) ||
(c> 11296 && c< 11311) ||
(c> 25600 && c< 26111) || (c== 7825)))
First a is evaluated, if (a == true) evaluated to true, then only it will execute next && statement
((b == 460) || (b == 454) || (b == 455) ||
(c> 13568 && c< 14335) ||
(c> 10640 && c< 10655) ||
(c> 11296 && c< 11311) ||
(c> 25600 && c< 26111) || (c== 7825))
Inside this, it will check for any one condition which is true, and once it encounter any one statement true, it return from there.
For your condition to be true, a must be true, and in addition, at least one of the conditions on b or c must be true.
Therefore, if a==true and c==254, you will get false, since c is not within any of the ranges you allow, and, as you said, b is not available (which I'm assuming means it doesn't have one of the 3 values you allow).
It would be much simpler if the code is written in a more readable manner;
bool isEqualToAny(int valueToCheck, int[] listToCheckIn){
boolean isMatch = false;
(for item in listToCheckIn){
if (item == valueToCheck){
isMatch = true;
break;
}
}
}
bool isWithinRange(int valueToCheck, int min, int max){
return (valueToCheck > min && valueToCheck < max);
}
if ((a == true)
&& (isEqualToAny(b, int[]{460,454,455})
|| isWithinRange(c,3568,14335)
|| isWithinRange(c,10640,10655)
|| isWithinRange(c,11296,11311)
|| isWithinRange(c,25600,26111)
|| isWithinRange(c,10640,10655)
|| (c== 7825)))
In java8 you can use an array of Tuples to make #isWithinRange more like #isEqualToAny

Multiple conditions in while; second condition never applies

I have a compound statement as follows:
for (int count = 0; count <= passLength; count++)
{
while( !(charGenerator >= 65 && charGenerator <= 90) || (charGenerator >= 97 && charGenerator <= 122))
{
charGenerator = randNum.nextInt(123);
}
System.out.print((char)charGenerator);
charGenerator = 0;
}
I have it within the for loop so it will generate many numbers at once, but my problem is it never picks numbers within the second range, in other words I never get any numbers from 97 to 122. It works fine with the first range. How do I make it so that it chooses random number from both groups?
The ! applies to the first term of the ||, not to the entire expression. Add a pair of parentheses:
while( !((charGenerator >= 65 && charGenerator <= 90) ||
(charGenerator >= 97 && charGenerator <= 122)) )

Categories

Resources