I want to split a 4-digit integer in to 2. i.e convert 1234
into two variables; x=12
and y=34
. Using Java.

- 51
- 1
- 1
- 2
-
10Welcome to Stack Overflow! We encourage you to [research your questions](http://stackoverflow.com/questions/how-to-ask). If you've [tried something already](http://whathaveyoutried.com/), please add it to the question - if not, research and attempt your question first, and then come back. – Aug 10 '12 at 10:51
-
2Ok, what ever you want is fine with me. Do you have a question? Have you tried something and it didn't work, but you are too embarrassed to show what you tried? – Peter Lawrey Aug 10 '12 at 10:52
-
4`a = 1234; x = a / 100; y = a % 100;` – Sergey Vyacheslavovich Brunov Aug 10 '12 at 10:52
-
+1 for Tichodroma's comment really we should be posting research problems. Problems like these can be found in so may web resources and text books. Thsese kind of problems are based on simple algorithms that need basic knowledge of any language you would like to implement it in :) – Sanyam Goel Aug 10 '12 at 10:56
-
What about the sign of the integer? `-1234` is still a 4-digit integer. – maba Aug 10 '12 at 11:11
7 Answers
int four = 1234;
int first = four / 100;
int second = four % 100;
the first one works because integers are always rounded down, stripping the last two digits when divided by 100.
the second one is called modulo, dividing by 100 and then taking the rest. this strips all digits exept the first two.
Lets say you have a variable number of digits:
int a = 1234, int x = 2, int y = 2;
int lengthoffirstblock = x;
int lengthofsecondblock = y;
int lengthofnumber = (a ==0) ? 1 : (int)Math.log10(a) + 1;
//getting the digit-count from a without string-conversion
How can I count the digits in an integer without a string cast?
int first = a / Math.pow(10 , (lengthofnumber - lengthoffirstblock));
int second = a % Math.pow(10 , lengthofsecondblock);
and at the end something usefull if you have cases where the input could be negative:
Math.abs(a);

- 1
- 1

- 1,188
- 2
- 11
- 26
int a = 1234;
int x = a / 100;
int y = a % 100;

- 17,291
- 7
- 48
- 81
-
1
-
@BryanH, oh, sorry. Types have been specified. There is nothing to explain - there are two lines of self-descriptive code. – Sergey Vyacheslavovich Brunov Aug 13 '12 at 05:40
You can treat it as a string and split it using substring(), or as an integer:
int s = 1234;
int x = s / 100;
int y = s % 100;
If it's originally an int, I'd keep it as an int and do the above.
Note that you need to consider what happens if your input is not four digit. e.g. 123.

- 268,207
- 37
- 334
- 440
In case you want to split the same no:
int number=1234;
int n,x,y; //(here n=1000,x=y=1)
int f1=(1234/n)*x; //(i.e. will be your first splitter part where you define x)
int f2=(1234%n)*y; //(secend splitter part where you will define y)
If you want to split the number in (12*x,34*y){where x=multiple/factor of 12 & y=multiple/factor of 34),then
1234=f(x(12),y(34))=f(36,68)
int number=1234;
int n; //(here n=1000)
int x=3;
int y=2;
int f1=(1234/n)*x; //(i.e. will be your first splitter part where you define x)
int f2=(1234%n)*y; //(secend splitter part where you will define y)

- 351
- 2
- 6
- 22
int num=1234;
String text=""+num;
String t1=text.substring(0, 2);
String t2=text.substring(2, 4);
int num1=Integer.valueOf(t1);
int num2=Integer.valueOf(t2);
System.out.println(num1+" "+num2);

- 1,305
- 11
- 12