Java – How to check if a string entered by the user contains a file name extension

How to check if a string entered by the user contains a file name extension… here is a solution to the problem.

How to check if a string entered by the user contains a file name extension

So in this program, the user can create/specify a directory and create a file, provided that the file does not exist. When users enter a file name, they also need to enter a file extension. The problem I’m having is how to get the code to look for the file extension at the end of the string entered by the user. I even checked”. “But I insist on a way for the program to see if the file has .json or .txt or”. “Anything after that

Long story short.
How to have the code check the file extension at the end of the string entered by the user (

Note that the code below is not finished yet, there are comments inside where I am stuck.

package filecreator.coolversion;

import java.io.File;
import java.io.IOException;
import java.util.*;

public class FileCreatorCoolversion {

public static Scanner sc = new Scanner(System.in);
public static boolean success = false;
public static String filename;
public static String filedir;
public static File file;
public static File dir;

public static void main(String[] args) throws IOException {

System.out.println("********************************");
    System.out.println("* Welcome to File Creator 2.0! *");
    System.out.println("********************************");
    System.out.println(" ");

while(!success) {

System.out.println("Would you like to create a file? Y/N?");
        String usrans = sc.nextLine();

if(usrans.equalsIgnoreCase("y")) {

System.out.println("Proceeding with file creation...");
            break;

} else if(usrans.equalsIgnoreCase("n")) {

System.out.println("Exiting Program...");
            System.exit(0);

} else if(!usrans.equalsIgnoreCase("y") || !usrans.equalsIgnoreCase("n")) {

System.out.println("That is not a valid answer! Please try again!");
            System.out.println(" ");
        }
    }

while(!success) {

System.out.println(" ");
        System.out.println("Please enter a valid filename:");
        filename = sc.nextLine();

if(filename.isEmpty()) {

System.out.println("Please enter a file name!");
            break;

} else if(filename.contains("/") || filename.contains(":") || 
                  filename.contains("*") || filename.contains("?") ||
                  filename.contains("<") || filename.contains(">") ||
                  filename.contains("|") || filename.contains("\"") ||
                  filename.contains("\\")) {

System.out.println("Please do not include / \\ : * ? \" < > |");

} else if(!filename.contains(".")) {

System.out.println("Please add a apropriate file extensions");

} else if (filename.) {

HERE IS WHERE IM STUCK

} else {

System.out.println(" ");
            System.out.println("File name \"" + filename + "\" chosen");
            break;
        }
    }

System.out.println(" ");
        System.out.println("Where would you like to have your file saved?");
        System.out.println("Please enter a valid directory");

while(!success) {

filedir = sc.nextLine();

if(!filename.contains(":")) {

System.out.println(" ");
            System.out.println("Please enter a valid directory!");

} else if(!filename.contains("\\")) {

System.out.println(" ");
            System.out.println("Please enter a valid directory!");

} else {

System.out.println("File directory \"" + filedir + "\" chosen");
            break;
        }
    }

System.out.println(" ");
    System.out.println("Creating file...");

}

Solution

All you might want to do is check the lastIndex of the . that you checked in the previous condition, and then see if there are any strings after it:

String extension = filename.substring(filename.lastIndexOf(".") + 1);
Checks if there is any extension after the last . in your input
if (!extension.isEmpty()) {
    System.out.print("This is the extension - " + extension);
}

Related Problems and Solutions