0

I am trying to convert an object from an object to an integer in java. These are the results I get with the following methods:

Object intObject = 50;
intObject: 50
intObject.toString(): 50
Integer.getInteger(intObject.toString()): null

I have no idea why I would get a null when converting the string of the object to an integer.

Here is the code that I am using:

    final Object temp = mCloudEntity.get(SaveAndLoadManager.TAG_TEST_INT);
    Log.i(TAG, "intObject: " + temp );  
    Log.i(TAG, "intObject.toString(): " + temp.toString()); 
    Log.i(TAG, "Integer.getInteger(intObject.toString()): " + Integer.getInteger(temp.toString()));
3
  • 2
    Please show some real Java code including your System.out.println's. Sort-of code just confuses us. Commented Jul 10, 2013 at 1:24
  • 1
    Please show the actual code you are running separate from the output that it produces. Commented Jul 10, 2013 at 1:25
  • Why you do not cast your object directly into int? Integer a = (Integer) intObject; Commented Jul 10, 2013 at 1:28

1 Answer 1

2

You should use

// Parses the string argument as a signed decimal integer
Integer.parseInt(intObject.toString()); 

instead of

// Determines the integer value of the system property with the specified name.
// (Hint: Not what you want)
Integer.getInteger(...) 
Sign up to request clarification or add additional context in comments.

3 Comments

It would help posting why to use this method instead.
Thanks @LuiggiMendoza, I updated with some snippets of the javadoc
Check the possible duplicated link to note a proper explanation on the subject. Refer to What is an acceptable answer? points 5, 8, 9, 10 and 11.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.