JAVA TIPS AND LOGICS


1. Conditional Operators


int value1 = 1;
int value2 = 2;
// AND Operator   "&&"
if((value1 == 1) && (value2 == 2))
// here Both statements are true condition is true other wise false
            System.out.println("value1 is 1 AND value2 is 2");
if((value1 == 2) && (value2 == 1))
            System.out.println("value1 is 2 AND value2 is 1");
// OR Operator   "||"
        if((value1 == 1) || (value2 == 1))
         // here Any one statements are true condition is true other wise false
            System.out.println("value1 is 1 OR value2 is 1");

    Output
             value1 is 1 AND value2 is 2
                   value1 is 1 OR  value2 is 1


2. Equality and Relational Operators

    int value1 = 1;
int value2 = 2;

         // Both values are equal this condition true
if (value1 == value2)
System.out.println("value1 == value2");
        // value1 not equal to value2 this condition is true
if (value1 != value2)
System.out.println("value1 != value2");              
if (value1 > value2)
System.out.println("value1 > value2");            
if (value1 < value2)
System.out.println("value1 < value2");
if (value1 <= value2)
System.out.println("value1 <= value2"); 

    Output
               value1 != value2
               value1 < value2
               value1 <= value2

3. Operators

     int result = 1 + 2; // result is now 3
        System.out.println(result);

        result = result - 1; // result is now 2
        System.out.println(result);

        result = result * 2; // result is now 4
        System.out.println(result);

        result = result / 2; // result is now 2
        System.out.println(result);

        result = result + 8; // result is now 10
        System.out.println(result);
        
        result = result % 7; // result is now 3
        System.out.println(result);   

    Output
             3 , 2, 4, 10, 3

4. if and else-if


        int testscore = 76;
        char grade;
        if (testscore >= 90) {
            grade = 'A';
        } else if (testscore >= 80) {
            grade = 'B';
        } else if (testscore >= 70) {
            grade = 'C';
        } else if (testscore >= 60) {
            grade = 'D';
        } else {
            grade = 'F';
        }
        System.out.println("Grade = " + grade);
  
    Output              

                   grade = 'C';

5. Loop While and for

        int count = 0;
        // While loop
        while (count < 11) {          
            System.out.println("Count while: " + count);
            count++;
        }            
           // for loop
            for(int i=0; i<11; i++){
                System.out.println("Count for: " + i);
        }

      Output
           Here Both outputs are same but conditions are different
                   Count while: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
                   Count for: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

6. for to loop through the array

int[] numbers = { 1,2,3,4,5,6,7,8,9,10 };
        for (int item : numbers) {
            System.out.println("Count is: " + item);
        }

    Output
           Count is: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

7. Number found in single Array-list(unlabeled break)

     // here number found in specific array list
int[] arrayOfInts =  { 32, 7, 3, 89, 12, 106, 20,  8012, 62, 51 };
       // search number is 12
        int searchfor = 12;
        int i;
        boolean foundIt = false;

        for (i = 0; i < arrayOfInts.length; i++) {
            if (arrayOfInts[i] == searchfor) {
                foundIt = true;
                break;
            }
        }
        if (foundIt) {
            System.out.println("Found " + searchfor + " at index " + i);
        } else {
            System.out.println(searchfor + " not in the array");
        }

      Output
             search number is in array list                     
                   // "Found " search number " at index " index number 
                   // here out put is number 12 this number in index 4
                      Found  12 at index  4
                   // number not found 
                      12  not in the array

8. for loops to search for a value in a two-dimensional array.

int[][] arrayOfInts = {      { 132, 817, 35, 589 },
                                                  { 12, 106, 200, 80 },
                                                  { 522, 12, 94, 150 }
                                          };
       int searchfor = 94;
       int i;
       int j = 0;
       boolean foundIt = false;
   search:
       for (i = 0; i < arrayOfInts.length; i++) {
           for (j = 0; j < arrayOfInts[i].length; j++) {
           
               if (arrayOfInts[i][j] == searchfor) {
                   foundIt = true;
                   break search;
               }
           }
       }
       if (foundIt) {
           System.out.println("Found " + searchfor + " at " + i + ", " + j);
       } else {
           System.out.println(searchfor + " not in the array");
       }

   Output
               // if 94 available in array list
                        Found 94 at 2, 2
                     // if specified number not in array list
                       number(94)  not in the array


9. program increments and find letter count in String.
 
String searchMe   = "peter piper picked a ppppp " +    "peck of pickled peppers";
int max = searchMe.length();
int letterCount = 0;

for (int i = 0; i < max; i++) {
// select specific letter here select "p" letter
if (searchMe.charAt(i) != 'p')
continue;  
letterCount++;
}
System.out.println("Found " + letterCount + " p's in the string."); 

      Output
            Found  14 p's in the string.

No comments:

Select DateRange UsingRangePicker.

  /* * This Method is for select range from picker. * */ private fun selectDateRangeUsingRangePicker () { pageNumber = 1 val displ...