-1

i'm trying to convert decimal to binary without any arrays or without recursion. I'm aware of the one command that you can use to convert decimal to binary but i'm trying to do this manually. Any help would be appreciated.

   public class Decimaltobinary {

    public static String decimalToBinary(int valueIn){

        //    String binaryOut = "";
        //    int counter = 0;
        int remainder, i = 0;

        while (valueIn != 0){
            remainder = valueIn % 2;
            valueIn /= 2;
            int binaryNum += remainder * i;
            i *= 10;
        }
        return binaryNum;

    }

    /**
    * @param args the command line arguments
    */

    public static void main(String[] args) {
        // TODO code application logic here
        Scanner keyboard = new Scanner (System.in);
        System.out.println("Please enter the decimal number: ");
        int valueIn = keyboard.nextInt ();
        String outputOut = decimalToBinary(valueIn);
        System.out.println ("The output is: " +outputOut);    
    }

}

I'm getting an error for the return BinaryNum statement that says 'cannot find symbol' and an error for the

 int binaryNum += remainder * i;

statement. The error says '; expected'.

3
  • 1
    You know about Integer.toBinaryString() right? Just checking to make sure you're re-implementing it on purpose. Commented Nov 5, 2018 at 18:54
  • So this method has real problems. Your method returns a string but you are trying to return in integer. Can't do that. Please fix the obvious syntax errors in your code first. Commented Nov 5, 2018 at 18:56
  • @markspace i do know about it yes. i don't want to use that function but rather, make one of my own. Commented Nov 5, 2018 at 18:59

1 Answer 1

1

you declared binaryNum inside while loop so the scope of this variable will be inside the loop only, declare it outside the while loop and change binaryNum type to String

public class Decimaltobinary {

public static String decimalToBinary(int valueIn){

     //    String binaryOut = "";
     //    int counter = 0;
      int remainder, i = 0;
       String binaryNum ="";
      while (valueIn != 0){
        remainder = valueIn % 2;
        valueIn /= 2;
        binaryNum = remainder+binaryNum;
      }
     return binaryNum;
}

/**
* @param args the command line arguments
*/

public static void main(String[] args) {
    // TODO code application logic here
    Scanner keyboard = new Scanner (System.in);
    System.out.println("Please enter the decimal number: ");
    int valueIn = keyboard.nextInt ();
    String outputOut = decimalToBinary(valueIn);
    System.out.println ("The output is: " +outputOut);    
   }

}
Sign up to request clarification or add additional context in comments.

10 Comments

that fixed the error, but its still giving me an error on the 'return binaryNum' command
change binaryNum type to string since your return type is string
okay that works great, thanks for that. however, if you input 10, it outputs 0000. it should be 00001010. what am i doing wrong?
updated you don't need to multiply with i always, this gets output in 4 bits
How would i do it differently?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.