Converting decimal to binary in Java

Integer.toBinaryString(int) should do the trick !

And by the way, correct your syntax, if you’re using Eclipse I’m sure he’s complaining about a lot of error.

Working code :

public class NumberConverter {
   public static void main(String[] args) {
       int i = Integer.parseInt(args[0]);
       toBinary(i);
   }

   public static void toBinary(int int1){
       System.out.println(int1 + " in binary is");
       System.out.println(Integer.toBinaryString(int1));
   }
}

Leave a Comment