package strings;
public class File {
String rootAdd;
int locateSlash,locateDot;
public File(String address){
setRoot(address);
}
public void setRoot(String address){
rootAdd = address;
}
public String getRoot(){
locateSlash = rootAdd.lastIndexOf("/");
return (rootAdd.substring(0, locateSlash));
}
public String getExt(){
locateDot = rootAdd.lastIndexOf(".");
return (rootAdd.substring(locateDot + 1));
}
public String getFileName(){
return (rootAdd.substring((locateSlash + 1), (locateDot)));
}
}
import java.util.Scanner;
public class FileRun {
static Scanner scan = new Scanner(System.in);
public static void main(String[] args){
File file1 = new File(scan.nextLine());
System.out.println("Root Address :- " + file1.getRoot());
try {
System.out.println("File Name :- " + file1.getFileName());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Extention Type :- " + file1.getExt());
System.out.println(file1.locateSlash);
System.out.println(file1.locateDot );
scan.close();
}
}
I added the exception just to continue the programe running cause i have a problem in the method getName();
I entered this argument, /home/user/index.html
and my stacktrace is - java.lang.StringIndexOutOfBoundsException: String index out of range: -11
at java.lang.String.substring(Unknown Source)
at strings.File.getFileName(File.java:22)
at strings.FileRun.main(FileRun.java:9)