-2

I made a program to calculate binary numbers, but it only reads input in base 10, so when I try to add two numbers together, the program adds them together as base ten numbers, and then converts them to binary, which results in it printing a huge binary number. Is there a way to get Java to read the input in base 2 rather than base 10?

import java.util.Scanner;
import java.lang.Integer;
public class Main
{
    public static void main(String[] args)
    {
        String answer="";
        Scanner scan=new Scanner(System.in);
        System.out.println("Please enter the first binary number:");
        String binary1=scan.nextLine();
        System.out.println("Please enter the second binary number:");
        String binary2=scan.nextLine();
        //both inputs are converted to base 10 ints, and added together, result is assigned to sum
        int sum=(java.lang.Integer.parseInt(binary1,10)+java.lang.Integer.parseInt(binary2,10));
        //sum is converted to a Binary String and assigned to answer
        answer=java.lang.Integer.toBinaryString(sum);
        System.out.println(answer);
    }
}
ndc85430
  • 1,395
  • 3
  • 11
  • 17
  • 1
    Why are you passing `10` for the `radix` argument of `parseInt` instead of `2`? – ndc85430 Aug 07 '22 at 05:07
  • @ndc85430 because I'm adding the inputs together as base 10 ten numbers, and converting the sum back to binary. The inputs are supposed to be in binary, but as I mentioned I don't know if theres a way to make Java read the inputs in base 2 rather then base 10. – Hydrolox3552 Aug 07 '22 at 05:17
  • Does this answer your question? [Converting Decimal to Binary Java](https://stackoverflow.com/questions/14784630/converting-decimal-to-binary-java) – AddeusExMachina Aug 07 '22 at 09:40
  • @Hydrolox3552 The way is to pass 2 instead of 10. The base or radix is what determines how an integer is converted to or from string, it doesn't have anything to do with the internal representation of an integer inside Java itself. – Mark Rotteveel Aug 07 '22 at 11:33

1 Answers1

2

You are misunderstanding what Integer.parseInt(binary1, 10) actually does.

It takes a String containing base 10 digits and converts it into an integer.

But ... obviously ... you don't have base 10 (decimal) digits. You are reading a number that has been expressed as base 2 (binary). So you should be using Integer.parseInt(binary1, 2)

The radix argument for parseInt is the radix of the input digits, not the radix of the number that you are producing.

(The number that you are producing is an int and an int is represented in two's complement signed binary in Java. Nothing you can do will change that. If you need a different integer representation you will need to inplement it yourself. Even BigInteger and BigDecimal are fundamentally signed, 2's complement binary ... when you use the parts of their API where their representation models are visible. The "decimal" in BigDecimal refers to the fact that is a number with base 10 scaling; i.e. a decimal point.)

I would note that feeding readLine() directly to parseInt will cause problems if your user types extraneous white space before or after the number. You should probably trim() the string, and you should probably catch any exceptions thrown by parseInt and tell the user what they did wrong.

Finally, you do not need to qualify the Integer class. All classes in java.lang are implicitly imported, which means that you don't need to qualify them with the package name.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • yeah that fixed it, my bad I really should have read the info on the method more closely. Thanks though I appreciate it. – Hydrolox3552 Aug 07 '22 at 05:30