1

This isn't working the way I expect it to. Sample input will include an asterisk that will resemble ether 0 or 1. The rest is comparing to see if the numbers match up. When I try to see if a character an asterisk it doesn't work the way I expect it to.

Samples:

org = *01

bit = 001 || 101 || 011

public static void doesItWork(String org, String bit)
    {
        for(int i = 0; i <= org.length() - 1; i++)
        {
            System.out.println(org.substring(i, i + 1));
            if(org.substring(i, i + 1) != "*" && org.substring(i, i + 1) !=  bit.substring(i, i + 1))
                break;

            if(i == org.length() - 1)
                System.out.println(bit);
        }
    }

Thanks guys for your help.

Kevin Read
  • 37
  • 3

2 Answers2

7

!= on Strings doesn't do what you want it to do. Java String.equals versus == explains, but in brief,

org.substring(i, i + 1) != "*"

should probably be

!org.substring(i, i + 1).equals("*")

or ideally

org.charAt(i) != '*'
Community
  • 1
  • 1
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
0

!= with Strings might not be good idea. Try !x.equals("*");

Ustaman Sangat
  • 1,505
  • 1
  • 14
  • 26
  • Also if you are a newcomer to java from a different language but know regular expressions, try them out in java, java.util.Regex – Ustaman Sangat Feb 10 '12 at 02:23