Java - Fill in the missing code to get File Extension

Question

Write code to get File Extension

Fill in the missing code.

Program Structure

public class Main {
    public static void main(String[] argv) {
        String name = "book2s.com";
        System.out.println(getFileExtension(name));
    }

    public static String getFileExtension(String name) {

        String ext = "";
        if (name.contains(".")) {
            ext = //your code here
        }
        return ext;
    }
}

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String name = "book2s.com";
        System.out.println(getFileExtension(name));
    }//from ww w . j  a  v a  2  s  .  co m

    public static String getFileExtension(String name) {

        String ext = "";
        if (name.contains(".")) {
            ext = name.substring(name.lastIndexOf(".") + 1);
        }
        return ext;
    }
}

Related Exercise