0

I'm trying to write a code that will allow the user to input a number in binary (with a limit of 8 digits) using modulus. I have this so far:

import java.util.Scanner; public class Main {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter a Binary Number (8 digit limit)");
    int binaryNum = input.nextInt();
    int m = 0;
    int l;
    for (int n = 0; n <= 7; n++){
        l = (2^n);
        if(binaryNum%(10^(n+1)) == (10^n)){
            m = m + l;
            binaryNum = binaryNum - (10^n);
        }
    }
    System.out.println("Decimal: " + m);
}

}

I tried inputting the number 1001010, but whenever I did, it came out as "Decimal: 2" which is clearly wrong. Can someone help?

T. Rainer
  • 17
  • 1
  • 2
  • Ok...you are trying to do binary to decimal and using modulus. What!!! I believe binary to decimal is where you add 2^x where x is the position of bit 1 in the binary number. Modulus operation is the way to go for conversion from decimal to binary/octal/hex etc. So 1001010 is 2^6+2^3+2^1 = 64+8+2=74. Just to clarify the bit's location is counted from right to left starting at 0. Its different for decimal numbers but not sure if you want to factor that in. – digidude Oct 09 '17 at 16:14
  • 4
    The `^` operator in Java has nothing to do with exponentiation as you might think. See [What does the `^` operator do in Java?](https://stackoverflow.com/questions/1991380/what-does-the-operator-do-in-java) . – Thomas Fritsch Oct 09 '17 at 16:21
  • Getting the input as the decimal reading of a string of ones and zeroes, using `Scanner.nextInt()` is a pretty weird thing to do. Why not input it as a `String`? – slim Oct 09 '17 at 16:26
  • Actually that makes much more sense. I completely forgot about the Math.pow(); I'm an idiot. – T. Rainer Oct 09 '17 at 17:18

2 Answers2

0
import java.util.Scanner; 

public class Main {

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.println("Enter a Binary Number (8 digit limit)");

    int binaryNum = input.nextInt(); // reading binary number

    int m = 0; // to store decimal number

    int l = 0; // to manage power of 2
    int d = 0; // to store intermediate result

    while (binaryNum != 0){ // looping till all digits are over

        d = binaryNum % 10; // taking the last bit of the binary number

        d *= Math.pow(2, l++); // finding 2^l 

        binaryNum /= 10; // dividing the binary number by 10 to shift one bit

        m += d; // add the result to decimal 
    }

    System.out.println("Decimal: " + m); // display result
}
}

I have edited the code so that the binaryNum is converted in to decimal properly. Hope this helps.!!

Sreeram TP
  • 11,346
  • 7
  • 54
  • 108
  • I am not sold about the loop being 0 to <=7 as its max 8 digits and not always 8 digits. Can you change it to a while condition i.e. while(n>0) – digidude Oct 09 '17 at 16:20
  • I recommend you to try that out yourself. If any error occurs feel free to post it. I will edit the code to handle any number of digits. ** NB : Input cant exceed the limit of Integer ** – Sreeram TP Oct 09 '17 at 16:25
  • Accept the answer and close issue if it worked for you – Sreeram TP Oct 09 '17 at 17:46
-2

You can use Integer.parseInt to convert a string into an integer, if you do it using a base 2:

int decimalValue = Integer.parseInt(c, 2);

You should be able to get the binary value for it.

Example:

System.out.println(Integer.parseInt("1001010", 2));

Output: 74

All you need need to implement now is the restriction for the length of the string the user can, which you want to be 8, and you are good to go.

If you don't want to use the parseInt you can also do:

        int binaryNumber = 10201; //example input binary
         int decimal = 0;
            int p = 0;
            while(true){
              if(binaryNumber == 0){
                break;
              } else {
                  int temp = binaryNumber%10;
                  decimal += temp*Math.pow(2, p);
                  binaryNumber = binaryNumber/10;
                  p++;
               }
            }
        System.out.println(decimal);

In this case I am converting from an int, which I guess will be ok for you, you just need to accept an int on the Scanner instead of a String, or convert the String to an int and this will do what you need, I think you misused some of the operations on your implementation.

  • I guess the downvotes came before you added the code. Just telling about the library methods without explaining what was wrong with the original code/approach doesn't really help the person asking the question. :) – digidude Oct 09 '17 at 17:41