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);
}
}