Im trying to make a while loop that checks to see if a user inputted character is correct. If the character is incorrect, the loop is supposed to run until it is. However, when i enter this loop I cannot exit, even if i input the correct value. Any help would be appreciated!
if (userInputCheck == 'U' || userInputCheck == 'D' || userInputCheck == 'R' || userInputCheck == 'L'){
return userInputCheck;
}
else
while(userInputCheck != 'U' || userInputCheck != 'D' || userInputCheck != 'R' || userInputCheck != 'L'){
System.out.println("ERROR. Please enter U for up, D for down, R for right, or L for left");
String temp = keyboard.next();
userInputCheck = temp.charAt(0);
}
return userInputCheck;
A test like x != 'U' || x != 'D' will ALWAYS be true -- think about it. If the chracter entered is U the first test will be false, but the second will be true, so the whole thing will be true. You want use && instead of ||:
while(userInputCheck != 'U' && userInputCheck != 'D' && ...
Once you do this, there's also no need for the first if...
Related
I want to write the following if - else logic in Velocity
If $var1 == NONE
( If $subvar1 != 'null'
return True
else
return Failed_Sub1)
Else
If $subvar2 != 'null'
return True
else
return Failed_Sub2
So basically $subvar2 is only evaluated if $var != NONE and $subvar1 is only evaluated if $var == NONE
I tried something like
#if($var1 != 'NONE')
#if($subvar2 != 'null')True
#{else}Failed_Sub2
#end
#else
#if($subvar1 != 'null')True
#{else}Failed_Sub1
#end
#end
But its returning nothing to me. What am I doing wrong?
Do you want to avoid null values or strings containing 'null'?
In velocity, you can check for nulls using any non assigned reference, for instance $null :
#if($var1 == $null)
...
Otherwise than that, your code looks fine and nested #if statements are definitely possible.
Here's the relevant documentation.
Trying to filter a specific data range in AX and having trouble.
This is what I am trying to do, in pseudo code..
(start >= now-30 && (end == null || end >= now-30))
||
(end >= now-30 && (start == null || start >= now-30))
||
(start >= now-30 && end >= now-30)
Here is how I tried to do it
CLASS DECLARATION
QueryBuildRange filterDates;
DS INIT METHOD
filterDates=this.query().dataSourceName('LogTable').addRange(fieldNum(LogTable,startDateTime));
filterDates=this.query().dataSourceName('LogTable').addRange(fieldNum(LogTable,endDateTime));
DS EXECUTEQUERY METHOD
filterDates.value(strFmt("(%1 >= %3 && (%2 == %4 || %2 >= %3)) || (%2 >= %3 && (%1 == %4 || %1 >= %3)) || (%1 >= %3 && %2 >= %3)", fieldStr(LogTable, startDateTime), fieldStr(LogTable, endDateTime), currentTimeMinus30Mins, DateTimeUtil::minValue()));
AX seems to ignore pretty much any completex query I enter.
Thanks
Provided that the logic in your pseudo-code is correct, I still can see a few flaws in your DS EXECUTEQUERY METHOD:
The entire expression must be enclosed in parenthesis.
Each sub-expression must be enclosed in its own set of parenthesis.
DateTimeUtil::toStr(...) is missing.
Try changing your code as follows:
filterDates.value(strFmt("(((%1 >= %3) && ((%2 == %4) || (%2 >= %3))) || ((%2 >= %3) && ((%1 == %4) || (%1 >= %3))) || ((%1 >= %3) && (%2 >= %3)))",
fieldStr(LogTable, startDateTime),
fieldStr(LogTable, endDateTime),
DateTimeUtil::toStr(currentTimeMinus30Mins),
DateTimeUtil::toStr(DateTimeUtil::minValue())));
You should be able to simplify it a bit:
filterDates.value(strFmt("(((%1 >= %3) || (%1 == %4)) && ((%2 >= %3) || (%2 == %4)) && !((%1 == %4) && (%2 == %4)))",
fieldStr(LogTable, startDateTime),
fieldStr(LogTable, endDateTime),
DateTimeUtil::toStr(currentTimeMinus30Mins),
DateTimeUtil::toStr(DateTimeUtil::minValue())));
Also, I don't see any reason for initialising filterDates twice in DS INIT METHOD. You can actually use a different field in this range, it doesn't have to be startDateTime or endDateTime:
filterDates = SysQuery::findOrCreateRange(this.query().dataSourceTable(tableNum(LogTable)), fieldNum(LogTable, RecId));
P.S. I don't remember very well how utcDateTime values should be used in such expressions - as far as I remember, DateTimeUtil::toStr(...) should work.
I think your logic is flawed which may be why it is not working!
I guess you would like to do a point-in-time search with two date-time fields start and end, and end is blank if not ended.
To search for records valid 30 days ago the only needed condition is:
(start <= now-30 && (end == null || end >= now-30))
Implementented in executeQuery of the datasource:
public void executeQuery()
{
QueryBuildDataSource qds = this.query().dataSourceTable(tableNum(LogTable));
SysQuery::findOrCreateRange(qds, fieldNum(LogTable,StartDateTime)).value('..'+queryValue(currentTimeMinus30Mins));
SysQuery::findOrCreateRange(qds, fieldNum(LogTable,EndDateTime)).value('"",'+queryValue(currentTimeMinus30Mins+'..');
super();
}
The above works for date fields, and I think it will work for date-time fields also.
So I have a bunch of input textfields to calculate some stuff. These are doubles. I want that every time its not all filled in, it will display "Please enter all values" Below mentioned code does this for all fields, except the last one, here nitrogen. If i change them around to make another variable last this does not work. Instead it just makes the app give the result when the last field is entered.
So for example, no fields got a value, but when I type something into nitrogen it gives me the result. If I type something else into any of the other fields (Nitrogen blank) i get the correct message "Please enter all values."
How do I make it account for all fields, not just all except the last one?
if (temprature,methane,ethane,propane,nbutane,ibutane,oxygen,npetane,ipetane,nhexane,nitrogen == 0)
{
outputText.text = #"Please enter all values";
} else
{
outputText.text = resultString;
}
Try this, this should work if any of the fields are empty.
if (temprature == 0 || methane == 0 || ethane == 0 || propane == 0 || nbutane == 0 || ibutane == 0 || oxygen == 0 || npetane == 0 || ipetane == 0 || nhexane == 0 || nitrogen == 0)
{
outputText.text = #"Please enter all values";
} else
{
outputText.text = resultString;
}
Let's say that I want to check that one of two terms are effective, how do I express it?
if (value == 1 **--OR--** value == nil) {
do something;
}
||
eg: if (value == 1 || value == nil)
I wrote this:
if( a == -11 && b == -1 ){
{
if( a == -1) AND ( b == -1)...
But neither work, and I have the same problem with OR. How do I write expressions that include OR or AND?
You use && for “and”, and || for “or”.
(a == -11 && b == -1) is fine and correct. Objective-C uses all of the same logical operators as C. || is the logical-or operator.
if (a==-11 && b==-1) {
if perfectly legal in C, and therefore Objective-C.
Your second example is not C or Objective-C