# Thread: How to split the integer

1. Member
Join Date
Jul 2014
Posts
9
Rep Power
0

## How to split the integer

Hi I am trying to split an integer for example my input is 0590
I want the output as
0
5
9
0
How to do this please suggest.
Thanks

2. ## Re: How to split the integer

How are you doing it without a computer? Write down rules that even an idiot could follow, and you'll have an algorithm that you can then start thinking about translating into code.

Hint: You aren't really treating it like a number, are you? You're treating it more like a *String* of digits.

Alternatively, you could use mod and division to extract the numbers.

3. Member
Join Date
Jul 2014
Posts
9
Rep Power
0

## Re: How to split the integer

Ya i have dome by using mod and division , The issue here is its not taking first digit as 0 in 0590 . Im getting the result as
5
9
0

4. ## Re: How to split the integer

ints are stored in the computer's memory with lots of leading zeros. When an int is converted to a String ALL the zeros are removed. Otherwise an int could display like this: 0000000590

Post the code you are having problems with. How are you entering the value: 0590?

5. Member
Join Date
Jul 2014
Posts
9
Rep Power
0

## Re: How to split the integer

Here is the code
Java Code:
```String Str = "abc010590";
int STR1 = Integer.parseInt(Str.replaceAll("[\\D]", ""));
System.out.println(STR1);

if (STR1 < 0)
STR1 = -STR1;
List<Integer> digits = new LinkedList<Integer>();

while (STR1 > 0) {
digits.add(0, STR1 % 10);
STR1 = STR1 / 10;
}
int a = digits.get(0);
int b = digits.get(1);
System.out.println(a);
System.out.println(b);```

Result
1
0
Last edited by pratheshbv; 07-09-2014 at 08:09 PM.

6. ## Re: How to split the integer

Did you understand what I said about how computers store int values and leading 0s?

Please edit your post and wrap your code with code tags:
[code]
YOUR CODE GOES HERE
[/code]
to get highlighting and preserve formatting.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•