Ihv writtten a prog of file searching and the result is to be displayed in table
Im geting exception of stringindexoutofbound: String index out of range: -1
at java.lang.string.substring(unknown source)
if i changed the path E:/ to e:/programs (program is some folder)
then prog run correctly


but i want to search the whole drive. plz help me out................


here's the code-

import java.io.*;
import java.util.*;
import java.text.SimpleDateFormat;
import javax.swing.*;
import javax.swing.table.*;

public class search2
{
	static String z,l,w;
	static String y;
	static File f;
	static JFrame frame;
	static JPanel panel;
	static DefaultTableModel model;
	static JTable table;
	
   public static void InsertRows()
    {
    frame = new JFrame("Inserting rows in the table!");
    panel = new JPanel();
    String data[][] = {};
    String col[] = {"Name","Folder","Date Modified"};
    model = new DefaultTableModel(data,col);
    table = new JTable(model);
    
    panel.add(table);
    frame.add(panel);
    frame.setSize(300,300);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
	
	
public static void visit(File f2)
{
	if(f2.isDirectory())
	{
		String x[] = f2.list();
		for(int i=0;i<x.length;i++)
		{
		File f3 = new File(f2,x[i]);
		l = f3.getName();
		if(l.equals(y))
		{	
                          	          String p = f3.getParent();
                                          File f4 = new File(p);
                                          String u = f4.getName();
                                          long timestamp = f3.lastModified();
                                          Date d = new Date(timestamp);
				 
SimpleDateFormat sdf = new SimpleDateFormat( "EE yyyy/MM/dd hh:mm:ss aa zz" );
		       String display = sdf.format(d);
		      model.insertRow(table.getRowCount(),new Object[]{l,u,display});
                                }
		if(f3.isDirectory())
		{ 
			visit(new File(f2,x[i]));
		}
		else
		{
		                String a = f3.toString();
		int index = f3.getName().lastIndexOf('.');
					
                	String w = f3.getName().substring(0, index);
					
	                if(w.equals(y))
		{
		             String p = f3.getParent();
			File f4 = new File(p);
			String u = f4.getName();
			long timestamp = f3.lastModified();
		                Date d = new Date(timestamp);
			SimpleDateFormat sdf = new SimpleDateFormat( "EE yyyy/MM/dd hh:mm:ss aa zz" );
			String display = sdf.format(d);				model.insertRow(table.getRowCount(),new Object[]{x[i],u,display});
		    }
	                    }
                                    }
                       }
		
     }
     public static void main(String ar[])throws Exception 
     {
	System.out.println("enter file to be searched");
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	y = br.readLine();
	f = new File(y);
	File f1 = new File("E:/");
	InsertRows();
	visit(f1);
     }
}

im getting this error at line
1. at search2.visit()
String w = f3.getName().substring(0, index);
2. at search2.visit()
visit(new File(f2,x));
3. at search2.main()
visit(f1);

it will throw an exception whereever you dont have any extension for a file...
so when you are checking indexOf("."), thr you put and if/else clause if(index>0)
{
}else{
}

thanx for ur suggestion it had solved my problem


it will throw an exception whereever you dont have any extension for a file...
so when you are checking indexOf("."), thr you put and if/else clause if(index>0)
{
}else{
}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.