473,401 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

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_Pdfs_10_2011"); //enter the location where mps pdf files are kept
String [] fname=dir.list();
if(fname==null)
System.out.println("no files");
else{



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


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

//System.out.println("existing name "+pdffilename);
String newname=fname[i].substring(11,19)+".pdf";
File newfilename=new File("c:\\irla_pdf\\eme\\pdf1011\\"+newname);//enter the location where renamed pdf files are to be kept
//System.out.println("new filename "+newfilename);
if(pdffilename.renameTo(new File("c:\\irla_pdf\\eme\\pdf1011\\"+newname))){
//System.out.println("renamed");
}
else
System.out.println("error");
//System.out.println(fname[i]);
//System.out.println(newname);



}
}
}
}
Jan 5 '12 #1
1 2236
rajujrk
107 100+
you fetching the files from the folder "C:\\irla_pdf\\eme\\MPS_Pdfs_10_2011\\". this is fine, but the problem is in the below code

Expand|Select|Wrap|Line Numbers
  1. 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,19) will fail.

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

Expand|Select|Wrap|Line Numbers
  1. String newname = fname[i].substring(0,fname[i].lastIndexOf(".")-1) + ".pdf";
  2.  
Thanks,
Jan 5 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Lem | last post by:
I get the error Exception in thread "main" java.lang.NoClassDefFoundError when I type java app2 in the command prompt. I've tried moving to the jre directory and typed java c:\app2\app2, but it...
1
by: CM | last post by:
Hi, when i want connect me in my BD with a JSP (with this simple code), this exception is throw. Thank's for ur help Mathieu CODE of my JSP ---------------------
6
by: ganesh.m | last post by:
Hi, I am new to DB2. I am getting this error while loading the DB2Driver. I don't have any idea about where i might have gone wrong. please help me. Below is the stack trace. Stack Trace:...
22
oll3i
by: oll3i | last post by:
i get Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at Producent.main(producent.java:605) when i run it from bat @start "Supply Chain Management-Producer to...
6
by: Light | last post by:
Hi I m getting this error while i m trying to run my program and i don't understand why that happens. import java.util.*; public class Exercise6 {
4
by: jmitch89 | last post by:
I don't why I get this error: Exception in thread "main" java.lang.NoClassDefFoundError The statement below works just fine: java -cp...
1
by: shaikhussain | last post by:
public class EmployeeServicesTestCase { /** * @param args */ public static void main(String s)throws Exception { // TODO Auto-generated method stub BeanFactory beans=new...
2
by: David Rothabuer | last post by:
I work with an integration engine that is java based. All my scripting is javascripting and I've run into a problem that I can't find a solution to. I am getting this error when I attempt to read...
1
by: Buena Velasco | last post by:
I'm having trouble finding the error on my code. I know what "Exception thread ... ArrayIndexOutOfBounds... " means but I couldn't tell which part in the looping it is or in other. import...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.