user604995
user604995

Reputation: 13

The IF argument

            if (array[r][0].equals("Buy")){
                System.out.println(array[r][0]);
                for ( i = 0; i < 8; i++)              
                    Buys[r][i] = array[r][i];
            }
        }
    if (array[r][0]==("Sell")){
        System.out.println(array[r][0]);
        for ( i = 0; i < 8; i++)              
            Sales[r][i] =array[r][i];
    }  

i'm try to figure out how the argument works and the way i always use the argument is either ==, =<, =>, !=. but now i'm tryin to use a .equals for an array to work, but it seems there is no error. so am i havin any Syntax Error? or am i using it wrong?

For more info of what i was tryin to do, is i'm tryin to make the argument to see if it equals Buy or Sell, it goes thru. but i tried == and .equals ... doesn't do anything I TRIED ,equals on "buy" part it still wont' go thru

            if (array[r][0].equals("Buy")){
                System.out.println(array[r][0]);
                for ( i = 0; i < 8; i++)              
                    Buys[r][i] = array[r][i];
            }

i switched ALL to .equals, but for some reasong it forces me to go thru this line

if (array[r][0] != null)

before this

        if (array[r][0] != null)
        for ( r = 0; r < 165; r++){
            System.out.println(array[r][0]);
            if (array[r][0].equalsIgnoreCase("Buy")){
                System.out.println(array[r][0]);
                for ( i = 0; i < 8; i++)              
                    Buys[r][i] = array[r][i];
            }
        }
    if (array[r][0].equalsIgnoreCase("Sell")){
        System.out.println(array[r][0]);
        for ( i = 0; i < 8; i++)              
            Sales[r][i] =array[r][i];
    }        

so why do i need this line to make this work, cuz apparently it stops it to thru that IF statement

Upvotes: 1

Views: 170

Answers (7)

bas
bas

Reputation: 1678

The contents of the array (array[r][0]) could be empty. The default value of a Stringarray is null, and since null doesn't exist, one can't call functions on it.

But for both "Buy" and "Sell" you know they exist and cannot be null. Therefore it would be better to replace array[r][0].equals("Buy") with "Buy".equals(array[r][0])

That way you won't have to check in advance if array[r][0] is null, since the equals does this for you.

One of the reasons you can't use == to check for string equality is because the String is an object, and in this case the == operator checks whether the memory address is the same for both string. Most of the time this is not the case. The equals operator recognizes that the object is a String and checks whether the contents of the String-object are the same.

Upvotes: 0

James
James

Reputation: 9278

You should not use == to compare Strings in Java. Use String.equals, String.equalsIgnoreCase or String.compareTo

Upvotes: 0

Platinum Azure
Platinum Azure

Reputation: 46193

The argument in an if statement must be something that is either true or false. This means you can use any relational operator (==, !=, <, >, <=, >=) or any method that returns a boolean type.

All objects have an equals method available, which returns boolean and is supposed to return true if two objects are "equal", for however you like to define equality for a given kind of object. (For example, two strings that have the same length and characters are equal) So this is a perfectly valid argument for if statements.

Upvotes: 0

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81694

THe "argument" to if is a boolean expression -- an expression whose value is of type boolean, which can be either true or false. Those operators (==, etc) return boolean values, as does the equals() method, and many other methods like the contains() method in the List interface. You can also write boolean methods yourself.

Upvotes: 1

Bozho
Bozho

Reputation: 597114

You still have if (array[r][0]==("Sell")). Make it use equals(..). Strings in java can't be compared with ==

Note: of course, as every object, == comparison can be done. But with strings it won't work unless you are using interned strings (that is, the same object for the same string contents)

Upvotes: 0

Amir Afghani
Amir Afghani

Reputation: 38531

  1. Never name your arrays 'array'. It does not convey any meaningful information

  2. Strings are objects, not primitives. You should always compare them using .equals() unless you really know what you're doing. It's not a matter of syntax, it's a matter of semantics.

Upvotes: 0

Bala R
Bala R

Reputation: 108957

the syntax is

if(-something-that-evaluates-to-boolean)

so things that evaluate to boolean such as (a < 1) or (b == 1) can be uses. Also anything that returns boolean can also be used. In your case, equals() returns boolean and that's why it works and the usage is perfectly valid.

Upvotes: 0

Related Questions