Menlam Choden
Menlam Choden

Reputation: 49

what is the difference between these java codes with if-else ? : operator

class Rextester
{  
    public static void main(String[] args) {
        int maxArray = 0;
        int minArray = 0;
        Scanner input = new Scanner(System.in);
        int[] numArray = new int[300];
        for (int i = 0 ; i < 4 ; i++){
            int num = input.nextInt();
            if(maxArray>num){
                maxArray=maxArray;
            }else{
                maxArray=num;
            }
             if(minArray<num){
                minArray=minArray;
            }else{
                minArray=num;
            }
        }
        System.out.println(maxArray+" "+minArray);
    }
}

second code

public class Mshmaxminarray {

    public static void main(String[] args) {
        int maxArray = 0;
        int minArray = 0;
        Scanner input = new Scanner(System.in);
        int[] numArray = new int[300];
        for (int i = 0 ; i < 300 ; i++){
            int num = input.nextInt();
            maxArray = maxArray > num ? maxArray : num ;
            minArray = minArray < num ? minArray : num ;
        }
        System.out.println(maxArray+" "+minArray);
    }
}

can anyone tell me why the above two codes give different solution.

Highest is fine but minArray gives wrong solution. isn't "condition ? if : else" same as if and else?

I am trying to find highest and minimum number of array , both gives the highest but there is some problem with minimum.

If both are same why different solution? and if not what is the difference?

Upvotes: 0

Views: 93

Answers (1)

user2342558
user2342558

Reputation: 6703

These rows are different:

class Rextester:

for (int i = 0 ; i < 4 ; i++){

class Mshmaxminarray:

for (int i = 0 ; i < 300 ; i++){

Also, could you have wanted to write like this?

int[] numArray = new int[300];
for (int i = 0 ; i < numArray.length ; i++){

ternary operator

The ternary operator simply do the same as if (condition) then statement assignment else statement assignment.

As in the official documentation:

Another conditional operator is ?:, which can be thought of as shorthand for an if-then-else statement

"If someCondition is true, assign the value of value1 to result. Otherwise, assign the value of value2 to result."

result = someCondition ? value1 : value2;

A better approach:

Rextester:

package myPackage;

import java.util.Scanner;
import java.util.*;

import myPackage.*;

public class Rextester {

    public static void main(String[] args) {

        int[] minMax = MyUtils.scannerMinMax(4);

        System.out.println(minMax[0] + " " + minMax[1]);
    }
}

Mshmaxminarray:

package myPackage;

import java.util.Scanner;
import java.util.*;

import myPackage.*;

public class Mshmaxminarray {

    public static void main(String[] args) {

        int[] minMax = MyUtils.scannerMinMax(300);

        System.out.println(minMax[0] + " " + minMax[1]);
    }
}

MyUtils:

package myPackage;

import java.util.Scanner;
import java.util.*;

public class MyUtils {

    public static int[] scannerMinMax(int numInt)
    {
        int minArray = 0;
        int maxArray = 0;

        Scanner input = new Scanner(System.in);

        Integer[] numArray = new Integer[numInt];

        for (int i = 0 ; i < numArray.length ; i++) {
            numArray[i] = input.nextInt();
        }

        List<Integer> listNums = Arrays.asList(numArray);

        minArray = Collections.max(listNums);
        maxArray = Collections.min(listNums);

        return new int[] {minArray, maxArray};
    }
}

First of all, because of you make the same operations in two classes, it's better to create a dedicated method to do that work.

In that method store all user inputs in the array, then search for the min and max value.

I use Collections to get the min and max element in the array, after converting it in List.

Disclaimer: I cannot test the code, please tell me if it works.

Upvotes: 2

Related Questions