# Parsing Integers

Page 1 of 1

## 4 Replies - 437 Views - Last Post: 22 May 2012 - 06:25 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=280255&amp;s=480935697e758368a3c52763dbbccddc&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Ruru123

Reputation: 0
• 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: [[email protected]
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

• D.I.C Lover

Reputation: 1461
• Posts: 3,289
• 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;

### #3 Ruru123

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

## Re: Parsing Integers

Posted 22 May 2012 - 06:13 AM

Ryano121, 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?

### #4 Ryano121

• D.I.C Lover

Reputation: 1461
• Posts: 3,289
• 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.

### #5 Ruru123

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

## Re: Parsing Integers

Posted 22 May 2012 - 06:25 AM

Ryano121, 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