Multiple conditions in while; second condition never applies - java

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)) )

Related

How to define 00-90 when I have charAt each of it

What I need to do is write a program that makes the first character (which is charAt(0) )and the second character (which is charAt(1) ) to become a value that not exceeding 90 which is (0 ~ 90) , but I also have to define them as an independent digit , because my program will make it to invalid if it is other than a digit.
So for an example it will become invalid if I type in 91
and it will valid if I type in number between 0~90
but I have no idea how to do this...
if(Character.isDigit(loop1.charAt(0))&&
Character.isDigit(loop1.charAt(1)))
I have tried this ,but not working
if(Character.isDigit(loop1.charAt(0)) &&
Character.isDigit(loop1.charAt(1)) &&
((loop1 >= 0)&&(loop1 <= 90)))
also this one but this is not working( I have no idea what I'm doing)
if(Character.isDigit(loop1.charAt(0)) &&
(((int)loop1.charAt(0)) >= 0) && <=9
Character.isDigit(loop1.charAt(1)) &&
((int)loop1.charAt(1)) <= 9)
Please help me... thanks a million !
Assuming I understand your question, parse loop1 and test the values using a simple if check, like
int t = Integer.parseInt(loop1);
if (t < 0 || t > 90) {
System.out.println("Value outside accepted range.");
} else {
System.out.println("Value valid.");
}
If I am getting this right you want to convert the first two characters of a string into a number and check is that number bigger than 90. Also you want the digits to be stored in different variables(?). If so this code should do it:
int digit1 = loop1.charAt(0) - '0';
int digit2 = loop1.charAt(1) - '0';
int number = digit1 * 10 + digit2;
if ( number <= 90 && number >= 0 )
System.out.println("Input is good");
else
System.out.println("Input is bad");

How to check values in a string?

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;
}
}

How to calculate if there is intersection between 2 ranges of ints in Java

I have 2 maximum and minimum values, for example:
22 ------ 26 and 16 ------ 22 (OK, because of 22)
22 ------ 26 and 10 ------ 12 (FAILS)
22 ------ 30 and 29 ------ 33 (OK because of 29 and 30)
I want to know if there exists an intersection between the sets that represent the maximum and minimum, in Java.
I've tried to do it in a paper and got an if with 4 tests, but I found they fail:
if ((thisEntityLeftPosX <= anotherEntityLeftPosX && thisEntityRightPosX >= anotherEntityRightPosX)
|| (thisEntityLeftPosX >= anotherEntityLeftPosX && thisEntityRightPosX >= anotherEntityRightPosX)
|| (thisEntityLeftPosX <= anotherEntityLeftPosX && thisEntityRightPosX <= anotherEntityRightPosX)
|| (thisEntityLeftPosX >= anotherEntityLeftPosX && thisEntityRightPosX >= anotherEntityRightPosX)) {
Maybe there is an easier way.
Not a duplicate because this is not about a number contained in a set, but a set intersection.
Start thinking in the reverse problem: two intervals, that mathematically can be described as [amin, amax] and [bmin, bmax], do not overlap iff (amin > bmax OR bmin > amax).
So, by simple negation of boolean expressions, these intervals overlap if and only if: (amin <= bmax AND bmin <= amax).
The following should work, where a and b are your two ranges.
if (aMin <= bMax && aMax >= bMax ||
bMin <= aMax && bMax >= aMax)
{
// OVERLAP
}
You can do it as follows:
int left = Math.max(thisEntityLeftPosX,anotherEntityLeftPosX);
int right = Math.min(thisEntityRightPosX,anotherEntityRightPosX);
if(left <= right) {
//overlap
} else {
//don't overlap
}
jDoodle demo with your sample input
The idea is that you calculate some sort of intersection: the intersection starts as the last of the given two; and ends as the minimum of the two. There are elements in the intersection if the left is thus less than or equal to the right.
This program makes the assumption that thisEntityLeftPosX is smaller than thisEntityRightPosX. The same holds for the another.... In case the assumption doesn't hold. You can modify the code as:
int temp;
if(thisEntityLeftPosX > thisEntityRightPosX) {
temp = thisEntityLeftPosX;
thisEntityLeftPosX = thisEntityRightPosX;
thisEntityRightPosX = temp;
}
if(anotherEntityLeftPosX > anotherEntityRightPosX) {
temp = anotherEntityLeftPosX;
anotherEntityLeftPosX = anotherEntityRightPosX;
anotherEntityRightPosX = temp;
}
int left = Math.max(thisEntityLeftPosX,anotherEntityLeftPosX);
int right = Math.min(thisEntityRightPosX,anotherEntityRightPosX);
if(left <= right) {
//overlap
} else {
//don't overlap
}
A possible extra is that if the ranges intersect, you have immediately constructed the range of the intersection. For [22;30] and [29;33] this thus generates the new range [left;right]=[29;33].
Just using min/max :
if follwing condition is true - they don't intersect
maxA < minB || maxB < minA

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.

What am i missing in this java code?

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

Categories

Resources