1

I am trying to take a string and convert it into a long how ever I keep getting the error mentioned above

public long[] stringToLongDecrypt()
{
    long ciphertext[] = new long[elements.length];
    for(int i=0; i<ciphertext.length; i++)
    {
        ciphertext[i] = Long.parseLong(elements[i].trim(), 16);
    }
    return ciphertext;
}

Any ideas?

1
  • That's the correct behavior. What are you expecting from empty string. Commented Apr 23, 2014 at 4:25

2 Answers 2

1

You have to parse, only a valid numbers, empty String "" is not a valid number. So, you have to check it before parsing it. Otherwise, you will get NumberFormatException.

public long[] stringToLongDecrypt() {
    long ciphertext[] = new long[elements.length];
    for(int i=0; i<ciphertext.length; i++) {
       if(elements[i] != null && !elements[i].trim().isEmpty()) {
          ciphertext[i] = Long.parseLong(elements[i].trim(), 16);
       }
    }
    return ciphertext;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Long cannot parse input which is of String datatype. Try using the following instead:

 Long.valueOf(elements[i].trim()).longValue();

2 Comments

No matter what you do, parse or valueOf, you cannot convert an empty string into a number.
@Lesya Have you tested it before posting?

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.