Java - Write code to check if a file name has Extension

Requirements

Write code to check if a file name has Extension

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String filename = "book2s.com";
        String ext = "book2s.com";
        System.out.println(hasExtension(filename, ext));
    }// ww w  . java  2 s  .  co m

    public static boolean hasExtension(String filename, String ext) {
        int index = filename.lastIndexOf(".");
        if (index == -1)
            return (false);
        return (filename.substring(index + 1).compareTo(ext) == 0);
    }
}

Related Exercise