Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

java int question?

Status
Not open for further replies.

jasonwyz98

Programmer
Nov 24, 2003
11
US
Hello
hello

Here is what i wanna do, someone please help

int tmp = 11242003;

11 = month
24 = day
2003 = year

i want to get the individual digit or group of digits in this case out of the int tmp. Can someone explain how to accomplish this in java.

jason
thanks
 
What you could do is to convert the integer to a String or StringBuffer and then use the substring method the obtain the month, day and year

Example:

int tmp = 11242003;

StringBuffer sb = new StringBuffer(Integer.toString(tmp));

if(sb.length() == 7)
{
// for months < 10 we have to add a zero up front
sb.insert(0, &quot;0&quot;);
}

int month = Integer.parseInt(sb.substring(0,2));
int day = Integer.parseInt(sb.substring(2,4));
int year = Integer.parseInt(sb.substring(4,8));

System.out.println(month + &quot;-&quot; + day + &quot;-&quot; + year);
 
Maybe something like :

int tmp = 11242003;
String sTmp = tmp +&quot;&quot;;

System.err.println(sTmp.substring(0, 2));
 
int d = 11242003;

//1 get four last digits
int dL4 = d % 10000;

//2 get digits 3 and 4

int d34 = ((d - dL4)/10000) % 100;

//2 get digits 1 and 2

int d12 = ((d - d %1000000)/1000000;

Ion Filipski
1c.bmp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top