Converting a string to a Long (java) & Google's personID

0 views
Skip to first unread message

user198923 via StackOverflow

unread,
Sep 22, 2014, 7:14:22 PM9/22/14
to google-appengin...@googlegroups.com

I am trying to get the logged in google user's googleID. I am able to get this value as a String object, however I want to be able to store it as a Long object (as in a long integer).

However, when I do this I am greeted with the error:

java.lang.NumberFormatException: Invalid long: "123456789012345678901" at java.lang.Long.invalidLong(Long.java:124) at java.lang.Long.parse(Long.java:364) at java.lang.Long.parseLong(Long.java:352) at java.lang.Long.parseLong(Long.java:318) at java.lang.Long.valueOf(Long.java:476)

The string that I am trying to convert is 21 characters long. Wny is it throwing this error?? What can I do to resolve it?

My code is below

   if (Plus.PeopleApi.getCurrentPerson(m_gacGoogleApiClient) != null) {
                Person perGooglePerson = Plus.PeopleApi.getCurrentPerson(m_gacGoogleApiClient);

                String strGoogleID = perGooglePerson.getId();
                Long lonGoogleID = Long.valueOf(strGoogleID);

     }


Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/25984440/converting-a-string-to-a-long-java-googles-personid

Ted Hopp via StackOverflow

unread,
Sep 22, 2014, 7:19:22 PM9/22/14
to google-appengin...@googlegroups.com

The maximum long value is 263 - 1 = 9223372036854775807L. Your 21-digit number is way too large to fit in a long. To avoid the error, you can keep it as a String or you can use a BigInteger.



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/25984440/converting-a-string-to-a-long-java-googles-personid/25984489#25984489

drewmoore via StackOverflow

unread,
Sep 22, 2014, 7:19:25 PM9/22/14
to google-appengin...@googlegroups.com

Long.maxValue is 9,223,372,036,854,775,807.

The string you're trying to parse - 123,456,789,012,345,678,901 - is larger than that, hence your exception.



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/25984440/converting-a-string-to-a-long-java-googles-personid/25984498#25984498

user198923 via StackOverflow

unread,
Sep 22, 2014, 7:39:29 PM9/22/14
to google-appengin...@googlegroups.com

I am trying to get the logged in Google user's googleID. I am able to get this value as a String object, however I want to be able to store it as a Long object (as in a long integer).

However, when I do this I am greeted with the error:

java.lang.NumberFormatException: Invalid long: "123456789012345678901"
            at java.lang.Long.invalidLong(Long.java:124)
            at java.lang.Long.parse(Long.java:364)
            at java.lang.Long.parseLong(Long.java:352)
            at java.lang.Long.parseLong(Long.java:318)
            at java.lang.Long.valueOf(Long.java:476)

The string that I am trying to convert is 21 characters long. Why is it throwing this error? What can I do to resolve it?

My code is below

   if (Plus.PeopleApi.getCurrentPerson(m_gacGoogleApiClient) != null) {
       Person perGooglePerson = Plus.PeopleApi.getCurrentPerson(m_gacGoogleApiClient);

       String strGoogleID = perGooglePerson.getId();
       Long lonGoogleID = Long.valueOf(strGoogleID);
   }


Reply all
Reply to author
Forward
0 new messages