• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to get int value ?

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all ,

i'm here stuck with a problem... i have a student class ... i'm supposed to send the date of birth of each student from main .. & calculate the difference in age ..
i have taken date of birth as String DoB; ( so it'll be like 31.10.1979 )
if i have to calculate the difference i need to have the int value for each field i.e day.month & year.. i separated them using substring in String class .. but i dont know how to get the value of it ..

can anybody help me with this ..

thankx in advance
Akshatha


 
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about this?

int day = Integer.parseInt("31");
 
Akshatha Nayak
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thankx Dmitry Melnik , i had tried that . but i did it this way

String abc = new String("31.10.1979");
String day = abc.substring(0,2);
Integer Day = new Integer();
int daY = Day.parseInt(day);

but it didnt work ..
thankx alot again
Akshatha
 
Dmitry Melnik
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but it didnt work

How?
 
Akshatha Nayak
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but this did work ..
String abc = new String("31.10.1979");
String day = abc.substring(0,2);
String month = abc.substring(3,5);
String yr = abc.substring(6,10);

int Day = Integer.parseInt(day);
int Month = Integer.parseInt(month);
int Yr = Integer.parseInt(yr);



Akshatha
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
jawala,

Welcome to Javaranch

We'd like you to read the Javaranch Naming Policy and change your publicly displayed name (change it here) to comply with our unique rule. Thank you.
 
Akshatha Nayak
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankx Corey McGlone ,
sorry i didnt know abt JNP ..
 
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Akshatha,

You might want to check out the String.split() method if you want to do it the grass roots way.

If you want to leverage the full "Power Of Java tm" you should check out the java.text.SimpleDateFormat, java.util.Date and java.util.Calendar classes.

Jules
 
Akshatha Nayak
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankx alot Julian Kennedy ..
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi. akshatha

the basic funda u can use is " read each char from string obtain its int value using casting.. then subtract 48(as for machines following ascii code) for its ascii value of 0.so ascii value of '7' will be 55...so..the actual no is 55-48 ie 7.." do it for all sets of chars occuring each time when a '.' precedes...the code can be
int *extract(char *a)
{
int date,month,year,cnt;
date=month=year=cnt=0;

for(int i=0;a[i]!='\0';i++)
{
if(a[i]=='.')
{i++;
cnt++;
}
if(cnt==0)
{
date+=int(a[i])-48; //for machines following ascii code
date*=10;
}
if(cnt==1)
{
month+=int(a[i])-48;
month*=10;
}
if(cnt==2)
{
year+=int(a[i])-48;
year*=10;
}

}
int *b;
b[0]=date/10;
b[1]=month/10;
b[2]=year/10;
return b;
}
this function returns a pointer of ur wish..
well if u find a better way inform me plz.
 
reply
    Bookmark Topic Watch Topic
  • New Topic