java.lang.ArrayIndexOutOfBoundsException: 2 error message -


i writing code trying split string (01/04/2010) 3 different integers. when try run it, arrayindexoutofboundsexception error.

here code:

public date(string date) {      string dayz[];     dayz = date.split("/");      int m=integer.parseint(dayz[0]);     int d=integer.parseint(dayz[1]);     int y=integer.parseint(dayz[2]);     if(y<100) {          y=integer.parseint(dayz[2])+2000;     }      setcomponents(m,d,y);   } 

here setcomponents method:

 public void setcomponents(int month, int day, int year) {      if (month < 1 || month > 12 || year < min_year || day < 1 || day >     numdaysinmonth(month, year)) {         throw new illegalargumentexception();     }     this.month = month;     this.day = day;     this.year = year;  } 

any help?

you can use stringtokenizer. cleaner , simple use.

import java.util.stringtokenizer;  public class helloworld{     public static void main(string []args){         string a="01/04/2010";         stringtokenizer st= new stringtokenizer(a,"/");         int data[] = new int[3];         int count =0;         while(st.hasmoreelements())         {             data[count++]=integer.parseint(st.nexttoken());         }          for(int i=0;i<3;i++)             system.out.println(data[i]);     } } 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

inno setup - TLabel or TNewStaticText - change .Font.Style on Focus like Cursor changes with .Cursor -