0

I do know how to do it on paper. The problem is when i do it on code. I get this error:

incompatible types: possible lossy conversion from int to byte

I know my binary number is getting saved in a String and i want to change those 0's to 1's and viceverse which is why i have the if

here's my code:

import java.util.Scanner:

Public class decimalacomplemento1 {

    public static void main(String[] ar){
       Scanner decimal = new Scanner(System.in);
       int dividendo, base = 2, numero, resto;
       String binario = "";
       System.out.println("Ingrese numero");
       numero = decimal.nextInt();
       dividendo = numero;

       while(dividendo >= base){ 
                resto = dividendo % base; 
               binario = resto + binario; 
               dividendo = dividendo/base; 
       }
       binario = dividendo + binario; // el numero binario


       if (binario == 0) {
              binario = 1;
       }else{
              binario = 0;
       }

       System.out.println("En sistema binario " + numero + " se escribe " +     binario);
   }
}
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • It's not clear to me where there's a `byte` variable in that code. – Andy Turner Aug 30 '16 at 21:08
  • 2
    The error message I get is “Incompatible operand types String and int”. Because `binario` is a String and `0` is an int, and in Java you cannot compare those. – Ole V.V. Aug 30 '16 at 21:09
  • What does "sign-magnitud" mean? What are you trying to do? If input is `123`, "sign-magnitud" sounds like it should print `1.23e2`, but your code seems to be trying to output number in base-2, so why not just do `Integer.toString(123, 2)` which returns `1111011`? – Andreas Aug 30 '16 at 21:19
  • Sorry, i meant one-complement sign-magnitud is another assignment, i got confused. i'm just trying to convert decimal to binary and then transform those 0's to 1's and viceversa. @Andreas – Hugo Rogue Aug 30 '16 at 21:50

1 Answers1

0

To invert 0 to 1 and vice versa throughout your String, you may do:

    StringBuilder buf = new StringBuilder(binario);
    for (int ix = 0; ix < buf.length(); ix++) {
        if (buf.charAt(ix) == '0') {
            buf.setCharAt(ix, '1');
        } else {
            buf.setCharAt(ix, '0');
        }
    }
    binario = buf.toString();

With this change, if I enter 6 (binary 110) into your program, it prints:

En sistema binario 6 se escribe 001
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Thanks a lot, Ole! This works but it doesn't seem to work when i input a negative decimal value. if i enter -1 it gives me: 00 . ¿Is it my code? @Ole V.V – Hugo Rogue Aug 30 '16 at 21:48
  • I believe it’s your code (sorry). -1 in twos-complement is all 1s (so 32 1-bits in a Java int because it is 32 bits). If I enter -1 into you program, it assigns it first to `numero`, then to `dividendo`. SInce `base` is 2 and -1 is not greater than or equal to 2, it never enters the while loop. In the assignment to`binario` -1 is converted to the 2-character string `"-1"`. My code looks at each char, and since none of them is 0, they are both “inverted” into 0. – Ole V.V. Aug 30 '16 at 22:58