0

I'm doing a java project where I have to convert from binary numbers and decimals. I'm working on converting from decimal to binary right now. This is the bit of code that i have. I don't know how to loop the remainders multiple times in this code.

public static void decimalToBinary()   {
  Scanner input = new Scanner (System.in);
  System.out.println ("Input decimal number");
  String decimal = input.next();
  int x = Integer.parseInt(decimal);
  int remainder = x%2;
  char[] charArray = decimal.toCharArray();
}

I don't know how to continue this. Thank you for your answers so far but i want to complete this with an array thats the challegne of the problem.

user2070292
  • 117
  • 1
  • 3
  • 8
  • You will find the answer here (though you probably don't necessarily need the 0-padding): http://stackoverflow.com/questions/4421400/how-to-get-0-padded-binary-representation-of-an-integer-in-java – Simon Lehmann Feb 15 '13 at 14:15
  • I don't understand how the remainder would help you solve your problem. Would you describe your algorithm in pseudocode or English? – Aaron Kurtzhals Feb 15 '13 at 14:18
  • I the remainders are part of the binary number that comes out, but then we need to get the remainder of the remainder and so on. Printing that out the remainders give the binary number from decimal...thats the best i can explain my thought process – user2070292 Feb 15 '13 at 14:21
  • `Integer.parseInt()` already does decimal to binary conversion. The remainder of your code is pointless. Unclear what you're asking. – user207421 Jan 29 '19 at 00:53

6 Answers6

3

The Integer class has methods to do what you want.

public static void decimalToBinary(){
    Scanner input = new Scanner (System.in);
    System.out.println ("Input decimal number");
    String decimal = input.next();
    String binary = Integer.toBinaryString(Integer.parseInt(decimal));
}
Gilbert Le Blanc
  • 45,374
  • 5
  • 61
  • 107
2

You can use the static helper method in Integer to convert from decimal to a binary string, which will get you most of the way there:

String inBinary = Integer.toBinaryString(10);   // result will be 1010
Sean Landsman
  • 6,458
  • 2
  • 24
  • 29
1
public static void decimalToBinary(){
    Scanner input = new Scanner (System.in);
    while (!*terminatingCondition*) {
      System.out.println ("Input decimal number");
      String decimal = input.next();
      System.err.println(Integer.toString(new Integer(decimal), 2));
  }
}

Hope that helps...

hd1
  • 30,506
  • 4
  • 69
  • 81
1
public static void binaryPrint(int n) throws Exception
{
    if(n > 0)
    {
        binaryPrint(n/2);
        System.out.print(n%2);
    }
    else if(n < 0)
        throw new Exception();          
}
Zabador
  • 609
  • 3
  • 12
0
public static String binaryRepresentation(int i32)
{
   String binary;
   for(int i = 31; i >= 0; i--){
     if((i32 & (1 << i)) > 0) binary += "1";
     else binary += "0";
   }
   return binary;
}

//...

String representation = binaryRepresentation(x);

This is if you do not like Integer.toBinaryString(int)

Aniket Inge
  • 24,028
  • 4
  • 42
  • 74
0

I think you need something like this:

public static void decimalToBinary(){
    Scanner input = new Scanner (System.in);
    System.out.println ("Input decimal number");
    String decimal = input.next();
    int x = Integer.parseInt(decimal);
    int remainder = x%2;
    char[] charArray = decimal.toCharArray();
}

public static char[] findBinary(int decimal) {
    if (decimal == 0) { // base condition
        return new char[0];
    }

    int remainder = decimal % 2;
    char[] remainderCharArray = findBinary(remainder); // Use recursion
    char[] decimalCharArray = decimal.toCharArray();

    char[] resultCharArray = /* Combine two arrays */;

    return resultCharArray;
}

I'm not sure how the binary representation of the number and the remainder are combined, but you need to do that where I put /* Combine two arrays */.

If you're not familiar with the concept of recursion, I suggest you read up about it.

Niel de Wet
  • 6,210
  • 6
  • 50
  • 91