0

I'm making a program that imitates the tabulation method (Quine McCluskey) and I'm trying to convert the input in String format to Integer format and finally back to String (in Binary form) format.

I got this error:

Exception in thread "main" java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source)

and here's the line with the error:

String a = toBinary(Integer.parseInt(a1));

any tips on how should I better approach this conversion? Assuming that the input is correct (only numbers are on the String).

EDIT: I forgot to include this method..

public String toBinary(Integer input) {
    String binString = Integer.toBinaryString(input);

    while(binString.length()<8) //for PADDING zeroes up front
        binString = "0" + binString;    

    System.out.println("\nBinString: " + binString);
    return binString;
}
7
  • 2
    It is always important to read the stacktrace. It says the exception happens in java.lang.Integer.parseInt, thus the issue is not in toBinary, but rather with a1 which contains an empty String. You need to find our why. Commented Oct 23, 2018 at 15:23
  • Yes. I just included it for clarification. Some might say that the "to binary" string is ".toBinaryString". Also, I passed a value to a1 by these line: String var1 = elementBinValue(mainRec.get(i)); String var2 = elementBinValue(mainRec.get(k)); if(compareTo(var1, var2)==1 I don't want to include "elementBinValue" method since I already checked and it works fine and even returned the string I needed before passing it to "compareTo" method. Commented Oct 23, 2018 at 15:33
  • The stacktrace doesn't lie. Although you removed the line of the stacktrace where it mentions your own code, it is the first starting point. Find that line and find out what you really passes to java.lang.Integer.parseInt(..). You need to debug that, but Stackoverflow can't help you to debug your code. Commented Oct 23, 2018 at 15:38
  • Okay thanks. Just in case, this code String a = toBinary(Integer.parseInt(a1)); works, right? Commented Oct 23, 2018 at 15:44
  • Again, your stacktrace looks like a1 is the culprit, thus this line is generally fine, but during runtime a1 contains something you or your program doesn't expect. When you use toBinary(Integer.parseInt("1234")); instead, when everything would work without an exception. If it does, then you need to debug the source of a1s value and why it assigns an empty String. Maybe that code itself receives a value it doesn't expect and the empty String is just the initial/fallback value? Commented Oct 23, 2018 at 15:49

1 Answer 1

0

It turns out the code was fine. The main reason for the error was a1 which is returning nothing. Got errors on previous methods. Thanks

String a = toBinary(Integer.parseInt(a1));

a1 is a String type and it should not be empty in this line.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.