3

I recently asked a much too general and haphazard a question so I'll try and do it the proper way this time.

The task is: write a method returning the extension of the File, i.e. the letters after the last dot(.) in the filename If the filename is hello1.doc, then the method should return doc If there is no dot(.) in the filename, then the method should return “unknown extension”

I'm having trouble with the output of the code not showing anything, let alone the portion of the string that I want it to show

Here is my code:

public Boolean Ftype() {

    if
    (fileName.indexOf('.') != -1)
    {
        String x= fileName.toString();
        String[] y=x.split(".");


        System.out.println("File is of type "+ Arrays.toString(y));
        return true;
    }
    else
    {
        System.out.println("Unknown File Extension");
        return false;
    }


}

For f2 = "tests.doc" the output is File is of type [ ]

How can I get the code to output file is of type[doc] or doc?

Thanks in advance for your help,

Dan.

Edit: Apparently Splitting isn't the only way to extract '.doc', apologies if that invalidates my question or makes it too general etc.

0

6 Answers 6

8

You need to escape the dot:

String[] y=x.split("\\.");

Another solution is using String.lastIndexOf and String.substring as there is no need for regex.

Sign up to request clarification or add additional context in comments.

Comments

6

You need to escape the dot, as it is a regex metacharacter:

String[] y = x.split("\\.");

Other possibilities are to use a character class

String[] y = x.split("[.]");

or use Pattern.quote() to do the necessary escaping for you.

String[] y = x.split(Pattern.quote("."));

Above is a fix for your code. A more reasonable solution to your problem would be

if (fileName.indexOf('.') != -1) {
    System.out.println("File is of type "+ x.substring(x.lastIndexOf('.') + 1));
    return true;
} else {
    System.out.println("Unknown File Extension");
    return false;
}

Beware of files like foo.tar.gz, where the filename is foo and the extion tar.gz also contains a dot.

2 Comments

Thanks guys, but 'escaping the dot' gives File is of type [test, doc]
@DanWyer: Don't use Arrays.toString(y), use y[y.length - 1] But beware, this will only print gz if the input was foo.tar.gz.
1

You don't really regex for this job.

This code should work (without regex):

if (fileName.indexOf('.') > 0) {
    System.out.println("File is of type "+ filename.substring(filename.lastIndexOf('.')+1);
}
else {
    System.out.println("Unknown File Extension");
}

Comments

1
public Boolean FType() {
        String[] tokens = fileName.split("\\."); // Use regex
        if (tokens.length == 1) { // No dots
            System.err.println("Unknown File Extension");
            return false;
        }
        System.out.println("File is of type [" + tokens[tokens.length - 1] + "]");
        return true;
    }

You have to escape the "." character -> "\\."

This function split the fileName using "." and display the last token. So if the fileName is "test.something.doc", the result will be "doc".

Comments

1

In this case, following will give you out put as File is of type [File, doc] for File.doc

        String[] y=x.split("\\.");
        System.out.println("File is of type "+ Arrays.toString(y));

If you want to get extension only change your code as follows.

        String[] y=x.split("\\.");
        System.out.println("File is of type "+ y[1]);

But what happened if your file has name like my.File.doc? Then this way is not good. So it is better to use following.

    public static Boolean Ftype(String fileName) {
    if(fileName.lastIndexOf('.') != -1){
        String x= fileName.toString();
        String[] y=x.split("\\.");
        System.out.println("File is of type "+ y[y.length-1]);
        return true;
    }
    else
    {
        System.out.println("Unknown File Extension");
        return false;
    }

Comments

0

System.out.println("File is of type "+ y[y.length-1]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.