jeudi 28 octobre 2021

How to compare values with more or less

I have a dataframe and i want to compare variables in line 3 using if statement with the use of more or less

let's say i want to compare the same values in third column with more or less 0.2

data >
NAME         A      B      C      D
first        3      2      4      5
second       1      2      3      4
third        7      7.1    7.5    6.9
four         2      1      0      5

here a program to compare the exact values

for (i in 1:3) {
  d <- i+1
  for (j in d:4) {
    if(data [3,i] == data [3,j] ){
        print(paste("The columns" , colnames(data[,i]) ,"and " , colnames(data[,i]) , "are equal"))
    }
  }
}

Here it retuns nothings because the program compares the exacte values and me i want to compare that have the same values more or less 0.2

the result i want is

the column A and B are equal  
the column A and D are equal

it's because A(=7) + or - the same as B(7.1) and the same thing for D A(=7) + or - D (6.9)

Thank you

Multiple if condition in java " if value exists in a list " [duplicate]

Is there another alternative to do this in java instead of listing values one by one ?

if ( x.equals("a") || x.equals("b") || x.equals("c") || x.equals("d") || x.equals("e") ){}

for example x exists in this list (a,b,c,d,e)

Is there a way that I can put two or more words string with spaces in if else statement?

I want to input something like without the snake cases or underscore, like only "Nothing is true." and not "Nothing_is_true." but every time I do that it's not reading the if statement.

Is there any other way to do it?

I'm new to coding, sorry if you find my question so dumb

public static void main(String[] args) {
    Scanner inp = new Scanner(System.in);
    String answer1;
    System.out.println("When men are blindly following the truth, remember...");
    answer1 = inp.next();
    if (answer1.equals("Nothing_is_true.")) {
        System.out.println("Correct.");
    } else System.out.println("Wrong.");
}

Else part of if/else statement not entered

I have the following bash script, which is used to open or close a relais depending whether certain applications are running or not.

The script basically works, so if all applications are running, the relais is closed. If one of the applications is not running, the else part for the statement should be entered. Unfortunately this doesn't happen, even if one of the applications return status "dead".

core_state=$(systemctl status auxiliocore.service | grep -o -P "(?<=active \().*(?=\))")
corerestapi_state=$(systemctl status auxiliocorerestapi.service | grep -o -P "(?<=active \().*(?=\))")
console_state=$(systemctl status auxilioconsole.service | grep -o -P "(?<=active \().*(?=\))")

echo $core_state
echo $corerestapi_state
echo $console_state

if [[ $core_state -eq "running" && $corerestapi_state -eq "running" && $console_state -eq "running" ]]; then
    echo $(date) "- all applications active."
    echo "1" >> /run/bspd/legacy/relais1
else
    echo $(date) "- at least one application is not running."
    echo "0" >> /run/bspd/legacy/relais1
fi

Replace If else in C Sharp

This is my Business Layer Code public void UpdateRecords() {

int resultCountries = 0
int resultState = 0
int resultDistrict = 0
int resultCity = 0
DataTable dt = dataAccess.GetRecords();
foreach(DataRow dr in dt.Rows)
{    
    int PersonId = Convert.ToInt32(dr["PersonId"].ToString());
    resultCountries = dataAccess.UpdateCountries(PersonId);
    if(resultCountries > 0)
    {
        dataAccess.UpdateMessage(personId,"Country Updated Successfully");
        resultState = UpdateState(personId);
        if(resultState > 0)
        {
            dataAccess.UpdateMessage(personId,"State Updated Successfully");    
            resultDistrict = UpdateDistrict(personId);
            if(resultDistrict > 0)
            {
                dataAccess.UpdateMessage(personId,"District Updated Successfully");
                resultCity = UpdateCity(personId);
                if(resutlCity > 0)
                {
                    dataAccess.UpdateMessage(personId,"City Updated Successfully");
                    continue;
                }
                else if(resutlCity == 0)
                {
                    dataAccess.UpdateMessage(personId,"City Not Updated Successfully");
                    continue;
                }
                else
                {
                    dataAccess.UpdateMessage(personId,"Some error occured while updating city...");
                    continue;            
                }        
        
            }
            else if(resultDistrict  == 0)
            {
                dataAccess.UpdateMessage(personId,"District not Updated Successfully");
                continue;
            }
            else
            {
                dataAccess.UpdateMessage(personId,"Some error occured while updating district..");
                continue;
            }        
        }
        else if(resultState  == 0)
        {
                dataAccess.UpdateMessage(personId,"State not Updated Successfully");
                continue;
        }
        else
        {
            dataAccess.UpdateMessage(personId,"Some error occured while updating state..");
            continue;
        }        
    }
    else if(resultCountries == 0)
    {
        dataAccess.UpdateMessage(personId,"Country not Updated Successfully");
        continue;
    }
    else 
    {
        dataAccess.UpdateMessage(personId,"Some error occured");
        continue;
    }
}

}

From business Layer I am calling data access layer methods,UpdateCountries,UpdateState,UpdateDistrict,UpdateCity which are updating country, state,district,city against personId in person table.

If country update is successful,state is updated otherwise log the error in db and update next record. Same is done for state,district,city. But here there are lots of if else used,kindly let me know how to replace if else in code. Do not want to use switch case or ternary operator,also not allowed to use stored procedure. The dataaccess layer methods return integer value of 1 if update is successfull,0 if condition is not satisfied,-1 if error.

mercredi 27 octobre 2021

Elseif PHP Codeigniter

Im new at codeigniter and i have a problem about if else on my datatable.

Why when i set date 2021-11-01 the result is ACTIVE? I think it should be ALERT.

this is my view

<?php
                $today   = Date("Y-m-d");
                $months  = Date("y-m-d", strtotime('+180 days'));   
    
                
                if ($t->tr_no == NULL) {
                echo 'NEW';
                }
    
                elseif ($t->tr_exp <= $today) {
                echo 'EXPIRED';
                }
    
                elseif ($t->tr_exp > $months) {
                echo 'ALERT';
                }
                
                else {
                echo 'ACTIVE';
                }
    
                ?>

Terminating program if the right input is inserted

So my problem is, I want to terminate the program if the right input was inserted, like A-E(big or smallcaps) and I want to ask them again the question if they input wrong letters like k,x,L. But my problem is, it wont stop asking even if the right letter is inputted.

#include <iostream>
#include <string>
using namespace std;
int main()
{
char letter;
char grades;
char A, B, C, D, E;
letter = A, B, C, D, E;
do{
cout << "\n Input grade letter: ";
cin >> letter;
if (letter=='A' || letter=='a')
{
    cout << "Your grade is in the range of 96-100";
}else if (letter=='B' || letter=='b')
{
    cout << "Your grade is in the range of 91-95";
}
else if (letter=='C' || letter=='c')
{
    cout << "Your grade is in the range of 86-90";
}
else if (letter=='D'|| letter=='d')
{
    cout << "Your grade is in the range of 80-85";
}
else if (letter=='E'|| letter=='e')
{
    cout << "Your grade is in the range of 79 and below";
}
else {
    cout << "Invalid Input! Try again!";
}
}while(letter==A, B, C, D, E);
///if the input correct
cout<< "\nBye";

return 0;

}