0

I'm trying to cast a string which is meant to be a BigInteger for key ID's in my mysql database calls for later. My JAVA is not the best, I can't seem to find a solution to turn my string back into a BigInt without getting any errors. Can someone please help?

String getWPU(final HttpServletRequest request, final HttpServletResponse    response) {
    BigInteger identifier = null
    def wpu = request.getParameter('slip')
    if (wpu?.size() > 0) {
        LOG.debug('using ID as user identifier: {}', wpu)
        identifier = new BigInteger(wpu);
    }


    identifier
}

I'm trying to follow solutions and don't get the expected result's. I'm feeling rather stupid today. Please put me out my misery. Here's my next attempt from reading around:

    def wpu = request.getParameter('slip')
    if (wpu?.size() > 0) {
        LOG.debug('using wordpress slip as user identifier: {}', wpu)

    }

    BigInteger identifier = new BigInteger(wpu);
    identifier

Which gives the following error when installing the JAVA project with Maven.

Cannot cast object '124' with class 'java.lang.String' to class 'java.math.BigInteger'

EDIT THE SOLUTION WAS THIS, as stated in duplicate answers but applied to my use case:

   def wpu = request.getParameter('slip')
    if (wpu?.size() > 0) {
        LOG.debug('using wordpress slip as user identifier: {}', wpu)

    }
    BigInteger identifier = new BigInteger(0);
    identifier.add(new BigInteger(wpu));
    identifier
Beloudest
  • 339
  • 1
  • 5
  • 18
  • 2
    *cast a string which is meant to be a BigInteger* doesn't make sense. You cannot cast a String to a BigInteger. Do you want to make a BigInteger out of a String representing a number? – Tunaki May 13 '16 at 13:21
  • Have you checked the API? [BigInteger](https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#BigInteger(java.lang.String)) What errors are you getting? – Clark Kent May 13 '16 at 13:21
  • 1
    Thanks, yes my string is actually a number. I wish to convert back from a string type. I'll check the API too. Thanks. – Beloudest May 13 '16 at 13:24
  • 1
    It would **really** help if you told us what the errors you are getting are. – Boris the Spider May 13 '16 at 13:25
  • 1
    @Akah I'm not too familiar with Groovy, but I believe this is Groovy. – Clark Kent May 13 '16 at 13:25
  • I've deleted my poor comment. – Akah May 13 '16 at 13:28

1 Answers1

2

You can try to use the BigInteger constructor:

BigInteger(String val)

Translates the decimal String representation of a BigInteger into a BigInteger.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • 1
    If this is the answer to the question, you may want to close as duplicate of http://stackoverflow.com/questions/15717240/string-to-biginteger-java instead of answering. See also http://meta.stackexchange.com/questions/10841/how-should-duplicate-questions-be-handled – Tunaki May 13 '16 at 13:27