Bhaswara Arizon
Bhaswara Arizon

Reputation: 11

How to check if String has a Dot, but not in front or in the end?

I'm currently practicing for my Java Practicum Exam, most importantly NachOS, here is my trouble right now. I'm stuck at developing this right now

Ask the user to input file name. The file name must contain dot (‘.’). Dot must not be in front of or in the end of the file name.

So far I've created the code like this

do {
        cs.write("Name: ");
        name = cs.read();
        Format = name.split(".");
    } while (Format.length!=1);

and the problem is that it still won't validate dot, even if I've typed the input "important.docx" for the example. can you tell why this happens and how I should solve this?

Upvotes: 0

Views: 986

Answers (1)

Patres
Patres

Reputation: 177

Keep it simple, Java has methods for that:

private static boolean validFileName(final String name) {
    return name.contains(".") && !name.startsWith(".") && !name.endsWith(".");
}

Upvotes: 3

Related Questions