0

This is the continuation of my last question: how to keep data stored into a hashtable - java

Here is the json:

{"DeviceID":"35181805087546548","TotalCount":"0","Timestamp":"2013-03-05 14:30:15"}

Here is the declaration of my hashtable:

    private static Hashtable<Integer,String> table = new Hashtable<Integer,String>();
    private static AtomicInteger count = new AtomicInteger() ;

Here is code to parse the jsonobject:

        JSONObject jsonObj;
        try {
            jsonObj = new JSONObject(string);
            int id = jsonObj.optInt("DeviceID", count.addAndGet(1) );
            String name = jsonObj.toString();
            table.put(id, name);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

Using the code below my id is always "2147483647" even if the "DeviceID" in the json changes. Any hint?

Many thanks

Community
  • 1
  • 1
Devester
  • 1,183
  • 4
  • 14
  • 41

3 Answers3

4

Your value is too large for an int. The largest allowable value is 2,147,483,647. See What is the maximum value for an int32?. You will need to parse the value as a long.

Community
  • 1
  • 1
Mike Hogan
  • 9,933
  • 9
  • 41
  • 71
1

The maximum value of an integer in Java is 2,147,483,647, so your device ID doesn't fit. You will probably need to store it as a long (maximum value 9,223,372,036,854,775,807).

Brendan Long
  • 53,280
  • 21
  • 146
  • 188
1

The number you are getting is the maximum value of a signed integer, you need to use long, or your result is limited to Integer.MAX_VALUE.

Chris Cooper
  • 4,982
  • 1
  • 17
  • 27