Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shankar sunwar
    New Member
    • Jan 2012
    • 1

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:

    i am getting this problem while running my program. Please help. The error report points at line no 17.

    import java.io.*;

    public class Rename{

    public static void main(String args[]){
    File dir=new File("C:\\irla_ pdf\\eme\\MPS_P dfs_10_2011"); //enter the location where mps pdf files are kept
    String [] fname=dir.list( );
    if(fname==null)
    System.out.prin tln("no files");
    else{



    for(int i=0;i<dir.lengt h();i++){


    File pdffilename=new File("C:\\irla_ pdf\\eme\\MPS_P dfs_10_2011\\"+ fname[i]); //enter the location where mps pdf files are kept

    //System.out.prin tln("existing name "+pdffilena me);
    String newname=fname[i].substring(11,1 9)+".pdf";
    File newfilename=new File("c:\\irla_ pdf\\eme\\pdf10 11\\"+newname);//enter the location where renamed pdf files are to be kept
    //System.out.prin tln("new filename "+newfilena me);
    if(pdffilename. renameTo(new File("c:\\irla_ pdf\\eme\\pdf10 11\\"+newname)) ){
    //System.out.prin tln("renamed");
    }
    else
    System.out.prin tln("error");
    //System.out.prin tln(fname[i]);
    //System.out.prin tln(newname);



    }
    }
    }
    }
  • rajujrk
    New Member
    • Aug 2008
    • 107

    #2
    you fetching the files from the folder "C:\\irla_pdf\\ eme\\MPS_Pdfs_1 0_2011\\". this is fine, but the problem is in the below code

    Code:
    String newname=fname[i].substring(11,19)+".pdf";
    Assume, inside your folder you have the file list like below

    ABCDEF.txt
    abc.txt
    java.txt

    for all these fname[i].substring(11,1 9) will fail.

    From your code, you are trying to rename the file extension. Do like this to rename the extension.

    Code:
    String newname = fname[i].substring(0,fname[i].lastIndexOf(".")-1) + ".pdf";
    Thanks,

    Comment

    Working...