-1

I have a file name reader from a folder.file`s names taken into a string array.what i want is when i executed this code segment i want the file name without its extension. But still extensions get printed out.

import java.io.File;
public class Main {

    public static void main(String[] args) {
        File file=new File("D:\\C_App\\PDF");
        String[] files = file.list();
        for(String string : files) {
            string.substring(0, string.length());
            System.out.println(string);
        }
    }
}

Can anyone help me to solve this? Thanks.

2
  • 2
    Just so you ALL know...A file name doesn't necessarily need to contain a file name extension. NONE of your answers takes this into account. @D.Anush - Check out the thread in this SO Post. Commented Aug 22, 2018 at 5:23
  • 1
    Strings are immutable in java. The substring method returns a new string that you need to assign to a variable. Commented Aug 22, 2018 at 8:45

3 Answers 3

1

Take substring of string from the last index of .

import java.io.File;
public class Main {

    public static void main(String[] args) {
        File file=new File("D:\\C_App\\PDF");
        String[] files = file.list();
        for(String string : files) {
             if(new File(string).isFile){ 
               if (string.lastIndexOf(".") > 0) {
                  System.out.println(string.substring(0, a.lastIndexOf('.'));
               }
            else {
               System.out.println(string);
            }
           }
        }
    }
}
Sign up to request clarification or add additional context in comments.

12 Comments

it gives Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at Main.main(Main.java:9). im very new to programming pls help
Can you print file name?
No nothing prints out except that exception.
Do System.out.println(string); and tell me the output.
File name get printed out with the extension.
|
0

Here is the Solution:

public static void main(String[] args) {
    File file = new File("D:\\SunilKanjar");
    String[] files = file.list();
    for (String string : files) {
        if (string.lastIndexOf(".") > 0) {
            System.out.println(string.substring(0, string.lastIndexOf(".")));
        }
    }
}

To get file name without extension first of all we need to find is there any extension or not and then need to remove extension only and for that find . at from the last.

It is not accurate to assume that file name not contain more then one . sign.

Comments

0

using regex by matching the last dot ( . ) and split,

the regex (\\.)(?!.*\\.) matches the last dot.

import java.io.File;
public class Main {
public static void main(String[] args) {
    File file=new File("D:\\C_App\\PDF");
    String[] files = file.list();
    for(String string : files) {
      if (!string.isEmpty()) {
        System.out.println(string.split("(\\.)(?!.*\\.)")[0]);
      }
    }
}

2 Comments

This would be the route I would of taken however I would of also added: if (new File(string).isDirectory()) { continue; } to ignore directories names within the list.
@D.Anush can you mark it an answer if it answers your question?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.