Adam Calcanes
Adam Calcanes

Reputation: 89

If statements aren't calling correct Strings or Booleans

Okay so basically I have a client that displays the median of an array IF it is in order This is checked in the server, IF the array is not in order then it produces an "Error message"but not the median. however it keeps processing the value of the median of the array although I created two conditions so that only IF(boolean = true then it displays the median)

Below is the client at the part in which I'm talking about'

        double median = Server.getMedian(array);

        Boolean truefalse = Server.checkFalse(array);
        // prints the response if the array is in order or not
        String response = Server.errorMsg(array);
        System.out.println(truefalse);
        if(truefalse = false){
            System.out.println(response);
        }
        else if(truefalse = true){
            System.out.println(response);
            System.out.println("The median of the array is " + median + ".");
        }
    }}
'

Here are the methods from the server. if( (array[i+1] < array[i]) || ((array[i+1] > array[i] && (array[0] > array[9]))) ){ This line above checks for ascending and descending order'

 public static String errorMsg(int[] array){
            String checker = "Error, the numbers are not in order";
            for(int i =0; i<array.length-1; i++){
                if( (array[i+1] < array[i]) || ((array[i+1] > array[i] && (array[1] > array[9]))) ){
                    return checker;
                }
            }
            return "The numbers are in order!";  
        }



        public static Boolean checkFalse(int[] array){
            for(int i =0; i<array.length-1; i++){
 if( (array[i+1] < array[i]) || ((array[i+1] > array[i] && (array[1] > array[9]))) ){
                    return false;
                }
            }
            return true;  
        }



    public static double getMedian(int[] array){
        double median;
        if (array.length%2 == 0){
    median = ((double)(array[(array.length/2)-1] + (double)array[array.length/2])/2);
        }
        else{
            median = (array[(array.length/2)-1]);
        }
        return median;
    }
'

This is what I get as responses from Java

false

Error, the numbers are not in order

The median of the array is 5.5. <---- this should not be there!

Upvotes: 0

Views: 75

Answers (3)

Makoto
Makoto

Reputation: 106508

= is assignment, not equivalence.

Now, you could switch all of your statements that look like:

if(truefalse = false)

to:

if(truefalse == false)

...but I would propose a better way. Since you already have some kind of boolean, why not just use it directly?

if(!truefalse)  //shorthand for truefalse is false

if(truefalse) //shorthand for truefalse is true

Upvotes: 0

TheLostMind
TheLostMind

Reputation: 36304

if(truefalse = false){
            System.out.println(response);
        }
        else if(truefalse = true){
            System.out.println(response);
            System.out.println("The median of the array is " + median + ".");
        }

is wrong... use

if(trueFalse)  //bad way of naming a boolean variable BTW

'=' assigns the value false/true to truefalse... It doesnt check..

Upvotes: 1

Zaheer Ahmed
Zaheer Ahmed

Reputation: 28588

== is used when comparing values.

= is the assignment operator, not a comparison operator.

try ==:

if(truefalse == false)

Upvotes: 0

Related Questions