0
public class Binary {

    private int _DecNum;
    private String _BinNum;

    public Binary(int n) {
    _DecNum = n;
    _BinNum = ConvToBinR(n);
    }

    public static String ConvToBin(int n) {
    String a = "";
    while(n > 1) {
        a = (n % 2) + a;
        n = n /2;
    }
    return n + a;
    }

    public String toString() {
    return _BinNum;
    }

    public boolean equals(Object a) {
    return this == a || (a instanceof Binary && this._BinNum == ((Binary) a)._BinNum);
    }

    public static void main(String[] args ) {
    int a = Integer.parseInt(args[0]);
    int b = Integer.parseInt(args[1]);
    Binary Zero = new Binary(a);
    Binary One = new Binary(b);
    System.out.println("Base 10: " + a + " in Base 2: " + Zero);
    System.out.println(Zero.equals(One));
    }
}

Hi Guys! My equals function does not work for some reason I can't figure out. this._BinNum == ((Binary) a)._BinNum; gives me a false statement even when I set the values of a and b to be equal. Can anyone help? Thank you!

Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85
user1844067
  • 61
  • 1
  • 6

6 Answers6

4

Just like you are using equals method to compare your instances, you should also use equals method to compare Strings: -

this._BinNum.equals(((Binary) a)._BinNum)

Also, I don't understand why you are having a this == a comparison in your equals method. That will always return false, unless you are invoking equals method on the same instance that you are passing. So you don't need that. Just use this: -

public boolean equals(Object a) {
    return (a instanceof Binary && this._BinNum.equals(((Binary) a)._BinNum));
}

It seems that you are trying to convert your Decimal value to Binary String. You already have a method in Integer class for that. Use Integer#toBinaryString or Integer#toString with radix: -

Integer.toBinaryString(5);
Integer.toString(5, 2);

This is of course for your future reference, as its ok to do it manually for learning purpose.


As a side note, you should follow Java Naming Conventions in your code, so that it is easily readable by other users in future.

  • Variable and Method names should start with lowercase alphabets, a dollar, or an underscore.
Community
  • 1
  • 1
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
4

_BinNum is a String so use

this._BinNum.equals(((Binary) a)._BinNum)
shazin
  • 21,379
  • 3
  • 54
  • 71
2

You should use equals() method for camparision of String and not == operator. == compares object references and not object contents

Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85
2

Those are two different objects, and their references are different and not equal.

Use the String's equals() method.

alex
  • 479,566
  • 201
  • 878
  • 984
1

You should try using stringA.equals(stringB) function for this

NoNaMe
  • 6,020
  • 30
  • 82
  • 110
1

the operator "==" will look whether the objects are equal or not based on the hash code of the objects. But, it doesm't look for the value in the objects.

whereas, equals() method will check for the value in the objects. So, try using stringA.equals(stringB)

Srinivas B
  • 1,821
  • 4
  • 17
  • 35