4 Replies - 506 Views - Last Post: 30 June 2011 - 08:43 PM Rate Topic: -----

#1 kaustubhdeo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-March 09

How to passed String array in Java

Posted 30 June 2011 - 10:25 AM

I tried to passed String array in java with following
    String addr[]=new String[2];
         addr[0]="MIDC,Pune";
         addr[1]="Nagzari,Amaravati";
        addr[2]="Navi Peth,Pune";
       Employee Jayesh=new Employee(1001,"Jayesh Damle",25000f,new Date(13,3,1998),new Date(14,5,1978),"Account",addr);
    


& collect the array of addresses from constructor of Employee like following
         public Employee(int empNo,String empName,float sal,Date joi_date,Date date_of_birth,String dept_info,String[] addresses)
      

but its giving error ArrayIndexOutOfBounds.

Is This A Good Question/Topic? 0
  • +

Replies To: How to passed String array in Java

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2675
  • View blog
  • Posts: 11,302
  • Joined: 20-September 08

Re: How to passed String array in Java

Posted 30 June 2011 - 10:36 AM

Quote

String addr[]=new String[2];


should be
String addr[]=new String[3];


Don't use deprecated Date ctors btw - use GregorianCalendar instead
Was This Post Helpful? 0
  • +
  • -

#3 Dev1462  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 20-May 10

Re: How to passed String array in Java

Posted 30 June 2011 - 10:58 AM

I believe its because somewhere in your Employee class you are iterating through the array with an index, that index is going 1 count too big for the array, may we see the employee class if that is ok?
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2675
  • View blog
  • Posts: 11,302
  • Joined: 20-September 08

Re: How to passed String array in Java

Posted 30 June 2011 - 11:01 AM

Incidentally 'n' in [n] is not the final index, but the number of elements the array contains
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: How to passed String array in Java

Posted 30 June 2011 - 08:43 PM

    String addr[]=new String[2];   // reserves room for addr[0] and addr[1]
         addr[0]="MIDC,Pune";
         addr[1]="Nagzari,Amaravati";
        addr[2]="Navi Peth,Pune";    // so out of bound


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1