mercredi 1 février 2017

Using Scanner library and if statment, simple task?

I need a little help. I'm learning Java and I'm stuck in one very simple exercise. Here is:

"Define the variables x and y as integers. The user needs to enter values for x and y. Write control data with if statement:

If x> 3, print "x is greater than 3"

If x <5, and y> 10, print "x is less than 5, and y greater than 10"

If y <10, print "y is less than 10"

If neither condition is not correct, print "condition is not correct''.

I've been using if statement, and simple input method where the program asks me to type my name, and after typing , program says "you're name is: ", but in this case I don't know how to combine library java.util.Scanner and method .next().

I Stuck here, am I doing right?

package exercise03;

import java.util.Scanner;
public class exercise03 {
public static void main(String[] args) {

Scanner input  = new Scanner(System.in);

   int x;
   int y;

   if(x>3){
       System.out.println("X greater than 3");
   }

   else if(x<5 && y>10){
       System.out.println("X is less than 5, and Y is greater than 10");
   }    

   else if(y<10){
       System.out.println("Y is less than 10");
   }

       System.out.println("Condition is not correct");

   }

}

PHP for each - else always returns else value

Im using the code below to send a report however when i add the else statement that is all that is returned even when the if statement is true.

can anyone explain why this? i dont think ive ever had this issue before?

        foreach ( $result as $page ) {
            $date1 = new DateTime($page->start_date);
            $date2 = new DateTime($page->end_date);

            if (strtotime($page->start_date) >= strtotime('today') && strtotime($page->start_date) < strtotime('last day of this month')) {

            $email_content .=  '<span style=" background-colour: #777777; font-size: 1em; font-family: arial, sans-serif; color:#202020;"><strong>' . $page->post_title . ' </strong></span>';

            $email_content .=  '<span style=" font-size: 1em; font-family: arial, sans-serif; color:#00b200;"><strong>Order By '  . $date1->format('d-m-y') . ' ' . '</strong></span>';

            $email_content .=  '<div style="  font-size: 1em; font-family: arial, sans-serif; color:#cc0000;"><strong>For Delivery '  . $date2->format('d-m-y') . '</strong></div><br>' . '<br>';

        }
            else {
                $email_content =  '<span style=" background-colour: #777777; font-size: 1em; font-family: arial, sans-serif; color:#202020;"><strong>tester </strong></span>';
            }
        }    

thanks for reading :)

For loop dependent on thread variables

My code may give some of you eye cancer, but please take a look:

for(V1=0;V1<4095;V1++){
send voltage signal to DAC
wait_ms(10);
    if(V1>1366){
        while(1){
          V2++;
          send voltage 2 signal to DAC channel 2;
          V1++;
          send voltage 1 signal to DAC channel 1;
          wait_ms(10);
              if(V1==4095){
               break;
              }
          }
    }
}

so when voltage 1 reaches 5V(1366), voltage 2 starts to increase along with voltage 1 until voltage 1 hits 12V(4095), and then they both stop. I type the exact same code but the conditions are angles measured in a previous thread, instead of V1.

thread* pThread=new Thread(angle_thread);
float angleY;
float angleX;
for(V1=0;V1<4095;V1++){
    send voltage signal to DAC
    wait_ms(10);
        if(angleY=something){
            while(1){
              V2++;
              send voltage 2 signal to DAC channel 2;
              V1++;
              send voltage 1 signal to DAC channel 1;
              wait_ms(10);
                  if(angleX=something){
                   break;
                  }
              }
        }
    }

What I want to happen is voltage 1 increases until angle Y is the desired angle, and then voltage 1&2 increase until angle X is the desired angle, and then the voltages stay at those values. What ends up happening is voltage 1 doesn't move until angle Y is the desired angle, and then it starts moving, and voltage 2 doesn't start at all.

Tldr, how do I control a loop using values that I got from a thread? They are declared globally. I hope my question makes sense

How to make SharedPreferences Condition?

How to make an optional condition with SharedPreferences? Example, like this code below. If i click "AnswerA" I will get the value of key A1_A. So,the value of key A1_B is null or 0. is it possible?

Because with the code i have below, i will get the all values. Thank you

  public void AnswerA() {
        sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
        img1= (ImageView) findViewById(R.id.img_01);
        img1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
           /*     PrefHelper.setInt(PrefKey.A1, option_scoreA1);*/
                SharedPreferences.Editor editor = sharedpreferences.edit();
                editor.putInt(A1_A, option_scoreA1);;
                editor.commit();
                Intent intent = new Intent(QuestionActivity.this, SecondActivity.class);
                startActivity(intent);
            }
        });
    }


public void AnswerB() {
    sharedpreferences = getSharedPreferences(MyPREFERENCE, Context.MODE_PRIVATE);
    img2= (ImageView) findViewById(R.id.img_02);
    img2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
       /*     PrefHelper.setInt(PrefKey.A1, option_scoreA1);*/
            SharedPreferences.Editor editor = sharedpreferences.edit();
            editor.putInt(A1_B, option_scoreA2);;
            editor.commit();
            Intent intent = new Intent(QuestionActivity.this, SecondActivity.class);
            startActivity(intent);
        }
    });
}

Autohotkey script - hotstring prevents if statement from working

:*:pl::pleaseHelpMe     ; hotstring

:*:rf::reallyConfused   ; hotstring

toggle := 0
\::
if (toggle = 0)
{
Run "http://ift.tt/LXtEj4"
}
return

The code above does not work for the keystroke '\'

However, the code below works without the hotstrings

toggle := 0
\::
if (toggle = 0)
{
Run "http://ift.tt/LXtEj4"
}
return

Why is that so? And how do I circumvent it and have the use of my hotstrings and the keystroke '\' to open google. Thanks a billion!!

Anyone out there willing look at my code to tell me why I've got an invisable ELSE?

This is real close to what I am using to create various widgets. I edited the code just enough that it does not matter if it is reused. I am going to be scripting this into PowerShell later, but for now I'll deal with what I've got. I am having issues with an invisible else "'else' is not recognized as an internal or external command." After the error the code is jumping to :CreatProdDir and skipping everything else.

@ECHO OFF

GOTO SetPathLocations

:SetPathLocations

    CLS
    SET JobPath="C:\NewCustomer\_Template"
    SET SamplesPath="C:\NewCustomer\_Samples"
    SET Type1Path="C:\NewCustomer\Product1"
    SET Type2Path="C:\NewCustomer\Product2"
    SET Type3Path="C:\NewCustomer\Product3"
    SET Type4Path="C:\NewCustomer\MULTOS"

PAUSE

GOTO CollectData

:CollectData

    SET /p CUSTName="Enter Customer Name (example: Mega_Warehouse or MegaWarehouse): "
    SET /p SHRTName="Enter Customer Name (example: MegaWH or MWH): "
    SET /p CustNum="Enter Customer Number: "
    SET /p PRODType="Enter Product1, Product2, Product3, Product4 or Product5: "
    SET /p RequestName="Enter new requested name: "
    SET /p PhoneNum="Enter the phone number: " 
    SET /p VerNum="Enter release version (v1.0.0, v2.0.0, etc...): "

PAUSE

GOTO SetNewPath

:SetNewPath

    IF /I PRODType==Product1 ( SET NewPath=%Type1Path%) 
    IF /I PRODType==Product2 ( SET NewPath=%Type2Path%)
    IF /I PRODType==Product3 ( SET NewPath=%Type3Path%)
    IF /I PRODType==Product4 ( SET NewPath=%Type4Path%)
    IF /I PRODType==Product4 ( SET NewPath=%Type4Path%)

PAUSE

GOTO CheckCustomer

:CheckCustomer

    IF EXIST %NewPath%\%CUSTName%\%PRODType%_%SHRTName%_%CustNum%_%VerNum%\nul GOTO Retry

PAUSE

GOTO Validate

:Validate

    IF /I PRODType==Product1 ( GOTO Product1 )
    IF /I PRODType==Product2 ( GOTO Product2 )
    IF /I PRODType==Product3 ( GOTO Product3 )
    IF /I PRODType==Product4 ( GOTO Product4 )
    IF /I PRODType==Product4 ( GOTO Product4 )

PAUSE

GOTO Retry

Type1Path="C:\NewCustomer\Product1"

:Product1

    MD "%NewPath%\%CUSTName%\%PRODType%_%SHRTName%_%CustNum%_%VerNum%"

PAUSE

GOTO CreatProdDir

Product2Path="C:\NewCustomer\Product2"

:Product2

    MD "%NewPath%\%CUSTName%\%PRODType%_%SHRTName%_%CustNum%_%VerNum%"
    Echo Creating working %CUSTName%....
    echo:
    xcopy "%NewPath%\Collection\*.*" "%NewPath%\%CUSTName%\%PRODType%_%SHRTName%_%CustNum%_%VerNum%\*.*" /h/e/c/k/y/r
    echo:
    xcopy "%NewPath%\Customer_TEMPLATE\*.*" "%NewPath%\%CUSTName%\TEMPLATE_%VerNum%\*.*" /h/e/c/k/y/r

    echo: This is to create a local directory structure 
    MD "C:\%PRODType%\%SHRTName%\Release"

    Echo Creating new file requirements for %CUSTName%
    echo f | xcopy "%JobPath%\Job.18888888888.xml" "%NewPath%\%CUSTName%\%PRODType%_%SHRTName%_%CustNum%_%VerNum%\Jobs\Job.%PhoneNum%.xml" /h/e/c/k/y/r
    echo f | xcopy "%SamplesPath%\Sample.txt" "%NewPath%\%CUSTName%\%SHRTName%_%VerNum%_%PRODType%_ProjectInfo.txt" /h/e/c/k/y/r
    echo f | xcopy "%SamplesPath%\Instructions.docx" "%NewPath%\%CUSTName%\%SHRTName%_%VerNum%_%PRODType%_Instructions.docx" /h/e/c/k/y/r

PAUSE

GOTO Exit

Type3Path="C:\NewCustomer\Product3"

:Product3

    MD "%NewPath%\%CUSTName%\%PRODType%_%SHRTName%_%CustNum%_%VerNum%"
    Echo Creating working %CUSTName%....
    echo:
    xcopy "%NewPath%\Collection\*.*" "%NewPath%\%CUSTName%\%PRODType%_%SHRTName%_%CustNum%_%VerNum%\*.*" /h/e/c/k/y/r
    echo:
    xcopy "%NewPath%\Customer_TEMPLATE\*.*" "%NewPath%\%CUSTName%\DataGen_%VerNum%\*.*" /h/e/c/k/y/r

    echo Creating new file requirements for %CUSTName%
    echo f | xcopy "%NewPath%\Job.18888888887.xml" "%NewPath%\%CUSTName%\%PRODType%_%SHRTName%_%CustNum%_%VerNum%\Jobs\Job.%PhoneNum%.xml" /h/e/c/k/y/r
    echo f | xcopy "%SamplesPath%\Sample.txt" "%NewPath%\%CUSTName%\%SHRTName%_%VerNum%_%PRODType%_ProjectInfo.txt" /h/e/c/k/y/r
    echo f | xcopy "%SamplesPath%\Instructions.docx" "%NewPath%\%CUSTName%\%SHRTName%_%VerNum%__%PRODType%_Instructions.docx" /h/e/c/k/y/r

PAUSE

GOTO RunPShell

Type1Path="C:\NewCustomer\Product4"

:Product4
    MD "%NewPath%\%CUSTName%\%PRODType%_%SHRTName%_%CustNum%_%VerNum%"
    Echo Creating %CUSTName% directory
    echo:
    xcopy "%NewPath%\Product4\*.*" "%NewPath%\%CUSTName%\%PRODType%_%SHRTName%_%CustNum%_%VerNum%\*.*" /h/e/c/k/y/r

    echo Creating new file requirements for %CUSTName%
    echo f | xcopy "%SamplesPath%\Product4_Instructions.docx" "%NewPath%\%CUSTName%\%SHRTName%_%VerNum%_%PRODType%_Instructions.docx" /h/e/c/k/y/r
    echo f | xcopy "%SamplesPath%\SampleProduct4Information.txt" "%NewPath%\%CUSTName%\%SHRTName%_%VerNum%_%PRODType%_Info.txt" /h/e/c/k/y/r

PAUSE

GOTO CreatProdDir

Type1Path="C:\NewCustomer\Prod5"

:Product5

    MD "%NewPath%\%CUSTName%\%PRODType%_%SHRTName%_%CustNum%_%VerNum%"
    Echo Creating %CUSTName% directory
    echo:
    xcopy "%NewPath%\Product5\*.*" "%NewPath%\%CUSTName%\%PRODType%_%SHRTName%_%CustNum%_%VerNum%\*.*" /h/e/c/k/y/r

    echo Creating new file requirements for %CUSTName%
    echo f | xcopy "%SamplesPath%\Product5_Instructions.docx" "%NewPath%\%CUSTName%\%SHRTName%_%VerNum%_%PRODType%_Instructions.docx" /h/e/c/k/y/r
    echo f | xcopy "%SamplesPath%\SampleProduct5Information.txt" "%NewPath%\%CUSTName%\%SHRTName%_%VerNum%_%PRODType%_Info.txt" /h/e/c/k/y/r

PAUSE

GOTO CreatProdDir

:CreatProdDir
    SET Proddir="%NewPath%\%CUSTName%\%PRODType%_%SHRTName%_%CustNum%_%VerNum%\ProductInfo"
    SET "ProdCount=1"
    SET /p ProdCount="Number of Products in Customer? (default: %ProdCount%): "
    for /L %%a in (1,1,%ProdCount%) do ( 
        SET /p PRODName="Enter %%a Product Number: " 
        call MD "%Proddir%\%%PRODName%%"
        )
    SET "ProdCount="

PAUSE

GOTO Exit

:RunPShell

powershell "$fileContents = Get-Content -Path %NewPath%\%CUSTName%\%PRODType%_%SHRTName%_%CustNum%_%VerNum%\Customer\%RequestName%.xml"
powershell "[xml]$xmlFile = $fileContents.Replace('CustNum="3503053131dfalj042898"','CustomerID="%CustNum%"')
powershell "[xml]$xmlFile = $fileContents.Replace('\\SHRTName\\','\\%SHRTName%\\)

PAUSE
GOTO exit

:Retry

    CLS
    echo:
    echo:
    echo "%NewPath%\%CUSTName%\%PRODType%_%SHRTName%_%CustNum%_%VerNum%"
    echo The customer and version already  EXIST.  You should make sure another Engineer
    echo has not already begun working on this customer.  IF you must take over you'll
    echo need to find that Engineer's copy of the document in order
    echo to pick up where the Engineer left off.
    echo:
    echo:
    SET /p StartOver="Start customer creation over^? (yes or no): "

PAUSE

GOTO StartOver

:StartOver

    IF /I StartOver==yes ( GOTO SetPathLocations ) 

PAUSE

GOTO ExitRetry

:Exit

CLS
echo:
echo:
echo Customer Creation completed for %CUSTName%
echo Find new release work in "%NewPath%\%CUSTName%\%PRODType%_%SHRTName%_%CustNum%_%VerNum%"
echo Find local folder and files in "C:\%PRODType%\%SHRTName%\Release"
echo Creatation Complete
echo:
echo:
PAUSE

:ExitRetry

CLS
PAUSE
exit

mongodb aggregate pipeline if else to execute one of the query

My objects looks like the following

{
    id: 1,
    a: "val7f",
    b: "online",
    c: "reg",
    d: "retd"
},
{
    id: 2,
    a: "val6f",
    b: "online",
    c: "reg",
    d: "new"
},
{
    id: 3,
    a: "val6f",
    b: "offline",
    c: "non-reg",
    d: "new"
}

I want to build a query in the following manner.

if (a=="val7f" && b=="offline" && c="reg") has objs > 1 return result Objs
else if(a=="val7f" && b=="offline" && c="reg") has objs > 1 return result objs
etc.

I built the following query but values displayed are from else query even if there are results for the if case:

db.Collection.aggregate([
    { $redact: {
        $cond: {
            if: { 
                $and: [ 
                    { $eq: [ "a", "val7f" ] }, 
                    { $eq: [ "b", "offline" ] }, 
                    { $eq: [ "c", "reg" ] } 
                ] 
            },
            then: "$$DESCEND",
            else: {
                $cond: {
                    if: { 
                        $and: [ 
                            { $eq: [ "$a", "val6f"] }, 
                            { $eq: [ "b", "offline"] }, 
                            { $eq: [ "c", "reg"] } 
                        ] 
                    },
                    else: "$$PRUNE"
                }
            }
        }
    } }
]);

Summarised as follows

I've put condition as follows

if: { 
    $and: [ 
            { $eq: [ "a", "val7f" ] }, 
            { $eq: [ "b", "offline" ] }, 
            { $eq: [ "c", "reg" ] } 
        ] 
    }  

if this has result say

{
    id: 6,
    a: "val7f",
    b: "online",
    c: "reg",
    d: "retd"
} 

it should return the above document. in case there are no document matching criteria

else case should execute

if: {
    $and: [ 
      { $eq: [ "$a", "val6f"] }, 
      { $eq: [ "b", "offline"] }, 
      { $eq: [ "c", "reg"] }
     ] 
    }  

and if there is match for this say

{
    id: 11,
    a: "val6f",
    b: "offline",
    c: "reg",
    d: "new"
} 

In my case, for the query built above I'm getting both the documents in the output shell.