6 Replies Latest reply: Apr 1, 2010 5:22 PM by 843789 RSS

    Comparing a charactor in a Binary String to a Binary  digit ("1" or "0")

    843789
      Hello, a newcomer here

      I'm trying to make an if statement inside a for statement
      something like

      /////
      01 for (int s=0; s< BinaryString.length; s++){                                                                          // The binary string is something like "00010"

      02 if (BinaryString.charAt(s) == "1") // gives me error "Incompatible operand types char and String"

      03 Line.getCellAt(StringCharCountB).setValue(Value.BLACK); //works fine
      04 else

      05 Line.getCellAt(StringCharCountB).setValue(Value.WHITE);} //works fine

      /////
      the actual code is more complicated than this, but i'm worried about this problem at the moment

      the part i'm finding difficulty with, is in line 02
      i know i'm doing it wrong. I tried changing "1" to '1', it didn't give me any errors before and after compiling, but the code in line 03 never works even if there was a character in the binary string that was "1"

      Can anyone help me point out the mistake i did, or suggest me the correct way to make my code work?

      Thanks
        • 1. Re: Comparing a charactor in a Binary String to a Binary  digit ("1" or "0")
          800606
          charAt() returns a char and "1" is a String. Use '1' instead.
          • 2. Re: Comparing a charactor in a Binary String to a Binary  digit ("1" or "0")
            843789
            Hello Alex
            I did try using '1' instead, but when i used it, the condition did not pass at all, be it BinaryString.charAt(s) == '1' was true or false
            it went imediatley to else
            • 3. Re: Comparing a charactor in a Binary String to a Binary  digit ("1" or "0")
              843789
              I tried using BinaryString.charAt(s) == 1 instead of '1'

              it gave a slightly different result (in my complicated version of the code (contains try catch statement for null strings, and two different strings to work on ( one is a result of ANDed set of strings(in a linked list), i need to set any 1 values in the resulted string to black on a line, and the other one is a result of Ored set of strings(in a linked list too), i need to set any 0 values in the string to white in the same line
              • 4. Re: Comparing a charactor in a Binary String to a Binary  digit ("1" or "0")
                3004
                Tehmomo wrote:
                I tried using BinaryString.charAt(s) == 1 instead of '1'
                No.
                '1' != 1
                '1' == 49 (I think)
                String bs = "00101100";
                
                for (int i = 0; i < bs.length; i++) {
                  if (bs.charAt(i) == '0') {
                    System.out.println("zero");
                  }
                  else if (bs.charAt(i) = '1') {
                    System.out.prinltn("one");
                  }
                  else {
                    throw new IllegalArgumentException("must be only 0 or 1");
                  }
                }
                • 5. Re: Comparing a charactor in a Binary String to a Binary  digit ("1" or "0")
                  3004
                  1. Always use braces with if, for, etc., even when not strictly necessary.

                  2. Provide an SSCCE.

                  3. When you post code, use code tags. Copy/paste from your original source in your editor, highlight the code, and click the CODE button. Use the Preview tab to see how your post will look.
                  • 6. Re: Comparing a charactor in a Binary String to a Binary  digit ("1" or "0")
                    843789
                    Thanks Jverd
                    I reworked my code.

                    I still don't get what i expect from the code which means i still have some mistakes to take care of

                    This is what i used to get after one pass

                    Link

                    This is what i get now, after one pass

                    Link

                    This is how it should actually look, after one pass

                    Link

                    this is the code i'm using for the try-catch, and the if else statements to set the black and white boxes
                    the gray ones are the unknowns. pass by pass, they get to be black or white depending on the constraints
                    try{          int StringCharCount =0;
                              Scanner scForBlack = new Scanner(SolvedBitBlackBinary);
                              Scanner scForWhite = new Scanner(SolvedBitWhiteBinary);
                              char c = scForBlack.nextLine().charAt(StringCharCount); // takes character input
                              char d = scForWhite.nextLine().charAt(StringCharCount); // takes character input
                              for ( StringCharCount =0;StringCharCount < SolvedBitBlackBinary.length(); StringCharCount++) {
                    
                                   
                                   if(c == '1'){
                                        System.out.println("c == 1");
                                        Column.getCellAt(StringCharCount).setValue(Value.BLACK);
                                   }
                                   if(d =='0'){
                                        System.out.println("d==0");
                                        Column.getCellAt(StringCharCount).setValue(Value.WHITE);
                                   }
                                   StringCharCount = StringCharCount+1;}
                    }
                    
                    catch ( RuntimeException e){
                         int StringCharCountB=0 ;
                         if (SolvedBitBlackBinary == null){
                              String OnePossibility = (String) List1.get(0);
                              Scanner scForAll = new Scanner(OnePossibility);
                              char b = scForAll.nextLine().charAt(StringCharCountB); // takes character input
                              for ( StringCharCountB = 0;StringCharCountB < OnePossibility.length();StringCharCountB++) {
                         if (b == '1'){
                              Column.getCellAt(StringCharCountB).setValue(Value.BLACK);
                         }
                         else{
                              Column.getCellAt(StringCharCountB).setValue(Value.WHITE);
                         }
                         StringCharCountB = StringCharCountB+1;
                         }}
                    }
                    By the way, the link to the site you posted isn't loading for me for some reason.

                    Thanks

                    Edited by: Tehmomo on Apr 1, 2010 3:20 PM