Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help fixing this error: Exception in thread main java.lang.NumberFormatException: For input string: under radix 2 at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67) at java.base/java.lang.Integer.parseInt(Integer.java:678) at DivideAndConquer.multiply(DivideAndConquer.java:8) at

I need help fixing this error:

Exception in thread "main" java.lang.NumberFormatException: For input string: "" under radix 2 at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67) at java.base/java.lang.Integer.parseInt(Integer.java:678) at DivideAndConquer.multiply(DivideAndConquer.java:8) at DivideAndConquer.multiply(DivideAndConquer.java:22) at DivideAndConquer.multiply(DivideAndConquer.java:23) at DivideAndConquer.multiply(DivideAndConquer.java:23) at DivideAndConquer.multiply(DivideAndConquer.java:21) at DivideAndConquer.main(DivideAndConquer.java:52)

/*******************************************************************************/

My java code:

import java.math.BigInteger;

public class DivideAndConquer {

public static String multiply(String x, String y) {

int n = Math.max(x.length(), y.length());

if (n == 1) {

int xi = Integer.parseInt(x, 2);

int yi = Integer.parseInt(y, 2);

return Integer.toBinaryString(xi * yi);

}

// Split x and y into left and right halves

int k = (n + 1) / 2;

String xl = x.substring(0, n - k);

String xr = x.substring(n - k);

String yl = y.substring(0, n - k);

String yr = y.substring(n - k);

// Recursively compute the three partial products

String p1 = multiply(xl, yl);

String p2 = multiply(xr, yr);

String p3 = multiply(addBinary(xl, xr), addBinary(yl, yr));

// Combine the partial products to get the final result

String t1 = padRight(p1, 2 * k);

String t2 = padRight(subtractBinary(subtractBinary(p3, p1), p2), k);

return addBinary(addBinary(t1, t2), p2);

}

public static String addBinary(String a, String b) {

BigInteger ai = new BigInteger(a, 2);

BigInteger bi = new BigInteger(b, 2);

BigInteger sum = ai.add(bi);

return sum.toString(2);

}

public static String subtractBinary(String a, String b) {

BigInteger ai = new BigInteger(a, 2);

BigInteger bi = new BigInteger(b, 2);

BigInteger diff = ai.subtract(bi);

return diff.toString(2);

}

public static String padRight(String s, int n) {

return String.format("%1$-" + n + "s", s).replace(' ', '0');

}

public static void main(String[] args) {

String x = "1001";

String y = "10010";

String result = multiply(x, y);

System.out.println(x + " * " + y + " = " + result);

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transact SQL Cookbook Help For Database Programmers

Authors: Ales Spetic, Jonathan Gennick

1st Edition

1565927567, 978-1565927568

More Books

Students also viewed these Databases questions