4 Replies - 236 Views - Last Post: 22 May 2012 - 06:25 AM Rate Topic: -----

#1 Ruru123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 09-October 11

Parsing Integers

Posted 22 May 2012 - 05:59 AM

Hi there,

I am trying to write a method that takes a parameter of type int and returning an integer array containing the digits that make up the parameter value.

	private int[] intToArray(int number) {
		int[] intArray;

		if (!(number > 0)) {
			return null;
		}
		String value = Integer.toString(number);
		char[] chars = value.toCharArray();
		int size = chars.length;
		intArray = new int[size];

		for (int i = 0; i < size; i++) {
			String entry = chars[i] + "";
			intArray[i] = Integer.parseInt(entry);
		}
		System.out.println(intArray);
		return intArray;	
	}




for example, i have initialised number to 145 so the method should return 1,4,5
however, my program returns a random number each time.. for example: [I@43256ea2
and I'm not quite sure where the problem is.

Thank you

This post has been edited by Ruru123: 22 May 2012 - 06:00 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Parsing Integers

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Parsing Integers

Posted 22 May 2012 - 06:07 AM

It's not a random value its the hashcode of the array that is generated when you call println on the array.

Instead try the Arrays.toString() method -

System.out.println(Arrays.toString(intArray);

return intArray;
Was This Post Helpful? 2
  • +
  • -

#3 Ruru123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 09-October 11

Re: Parsing Integers

Posted 22 May 2012 - 06:13 AM

View PostRyano121, on 22 May 2012 - 06:07 AM, said:

It's not a random value its the hashcode of the array that is generated when you call println on the array.

Instead try the Arrays.toString() method -

System.out.println(Arrays.toString(intArray);

return intArray;


Thank you. I tried that and got what I wanted.
But, why do I have to do that..why couldn't I just put the array name?
Was This Post Helpful? 0
  • +
  • -

#4 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Parsing Integers

Posted 22 May 2012 - 06:15 AM

It's because it's acts like any other object.

If you make a class and don't override the .toString method, and call println, it will give the hashcode like it did here.

All that's happening is we are calling the Arrays class .toString to give a visual representation of the object that is readable to the user.
Was This Post Helpful? 1
  • +
  • -

#5 Ruru123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 09-October 11

Re: Parsing Integers

Posted 22 May 2012 - 06:25 AM

View PostRyano121, on 22 May 2012 - 06:15 AM, said:

It's because it's acts like any other object.

If you make a class and don't override the .toString method, and call println, it will give the hashcode like it did here.

All that's happening is we are calling the Arrays class .toString to give a visual representation of the object that is readable to the user.



Oh I see. Thanks alot :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1