13 Replies Latest reply: Jul 22, 2007 3:36 AM by 807600

# help... how to separate the integer number..?

I want to separate the integer number into 3 parts.
for example 12085441 the parts become:
part 1 : 12
part 2 : 08
part 3 : 5441

Thanks....
• ###### 1. Re: help... how to separate the integer number..?
You can write your own class extending Integer which implements methods to get these parts from the integer number.

Try something like:
public class PartedInteger {
private int number;

public PartedInteger(int number) {
this.number = number;
}

public int getPart(int part) {
switch(part) {
case 1: return ...;
case 2: return ...;
case 3: return ...;
default: return ...;
}
}
}
• ###### 2. Re: help... how to separate the integer number..?
I got your idea, but how to write the code to separate that 8 digits integer number....?
• ###### 3. Re: help... how to separate the integer number..?
I think I can do it...
My code is:

public class test
{
public static void main(String args[])
{
int n = 23121888;
int part1 = n / 1000000;
int part2 = (n % 1000000 - y)/10000;
int part3 = n % 10000;

System.out.println("Part 1 : " + part1 + "\nPart2 : " + part2 + "\nPart3 : " + part3);
}
}
• ###### 4. Re: help... how to separate the integer number..?
I think I can do it...
My code is:

public class test
{
public static void main(String args[])
{
int n = 23121888;
int part1 = n / 1000000;
int part2 = (n % 1000000 - y)/10000;
int part3 = n % 10000;

System.out.println("Part 1 : " +
part1 + "\nPart2 : " + part2 + "\nPart3 : " +
part3);
}
where that y came from? and what do you mean you want to separate the integer into 3 parts? i mean is there any calculation required or its just a simple program where you would like to split an int into three different parts?
• ###### 5. Re: help... how to separate the integer number..?
simply convert it to string and split it in three different variables using substring function.
• ###### 6. Re: help... how to separate the integer number..?
I am a student, I am developing a simple program which required the date.
That 8 digits number will be stored in the Ms.Access database, and I will store it as text. When the date is loaded, I convert to int, then separate it become 3 parts,
part 1 for day, part 2 for month and part 3 for year.
The purpose of separating these number is to compare or calculate or date sorting purpose on my program. I create the Date ADT to receive the number (day, month, year), so I can calculate that date.

Sorry, its quite confusing... I just make simple program that implement ADT and Database...
• ###### 7. Re: help... how to separate the integer number..?
You can write your own class extending Integer
No he can't. java.lang.Integer is final
• ###### 8. Re: help... how to separate the integer number..?
I am a student, I am developing a simple program
which required the date.
That 8 digits number will be stored in the Ms.Access
database, and I will store it as text. When the date
is loaded, I convert to int, then separate it become
3 parts,
part 1 for day, part 2 for month and part 3 for
year.
The purpose of separating these number is to compare
or calculate or date sorting purpose on my program. I
month, year), so I can calculate that date.

Sorry, its quite confusing... I just make simple
program that implement ADT and Database...
If you're talking about dates, then they aren't integers. What date does the "integer" 1112001 represent? 1/11/2001 or 11/1/2001? Are you going to mandate that months January-October be stored with a leading '0'? How are you going to enforce that? How are you going to be sure it's been done? No need to represent a date as an integer, that's just asking for bugs
• ###### 9. Re: help... how to separate the integer number..?
I create just a simple date..
I create Date ADT (Abstract Data Type) for calculating and comparing the date, also to make the result sorted. In that ADT also has a method receiving the month number and return the String month.
Thats my own way to create a simple program just to show the implementation of ADT and Database.
• ###### 10. Re: help... how to separate the integer number..?
I create just a simple date..
I create Date ADT (Abstract Data Type) for
calculating and comparing the date, also to make the
result sorted. In that ADT also has a method
receiving the month number and return the String
month.
Thats my own way to create a simple program just to
show the implementation of ADT and Database.
That's as may be, but it's buggy
• ###### 11. Re: help... how to separate the integer number..?
You can look up the Calendar object in the JDK, or to make it easier you can see java.sql.Date which contains deprecated method helps doing what you want..(^_^);
• ###### 12. Re: help... how to separate the integer number..?
typecast that integer no. into char (array)....
and the print array element 0&1, then 2&3, then the rest.....
• ###### 13. Re: help... how to separate the integer number..?
typecast that integer no. into char (array)....
and the print array element 0&1, then 2&3, then the
rest.....
Did you bother to try that out, or even think whether it made sense, before posting it? Seriously, is there some "Who can come up with the most nonsensical solution" contest on this thread, that I don't know about??