[英]How to check if the string the user has entered contains a file extension
So in this program, a user is able to create/specify a directory and create a file, given that the file doesn't exist already. 因此,在此程序中,用户能够创建/指定目录并创建文件,因为该文件尚不存在。 when the user is entering a file name, they are required to enter a file extension as well.
当用户输入文件名时,他们也需要输入文件扩展名。 The problem I am having is how to make the code look for a file extension at the end of the string the user inputs.
我遇到的问题是如何使代码在用户输入的字符串末尾查找文件扩展名。 I went as far as checking for a "."
我竟然检查了一下“。” but I am stuck on a way that the program can see if the file has a .json or .txt or anything AFTER the "."
但我被困在程序可以看到文件是否有.json或.txt或“。”之后的任何内容的方式上。
TL;DR How do I make the code check for a file extension at the end of a string a user has inputted (<- not a word) TL; DR如何在用户输入的字符串末尾检查文件扩展名(< - 不是单词)
Please note that the below code is not complete yet, the condition where I'm stuck at has a comment inside. 请注意,下面的代码还没有完成,我坚持的条件在里面有评论。
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...");
}
} }
All you might want to do is check the lastIndex of the .
您可能想要做的就是检查的lastIndex
.
which you checked in the previous condition and then see if there is any string after that or not: 您在之前的条件中检查过,然后查看之后是否有任何字符串:
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);
}
Find the file extension if any: 找到文件扩展名(如果有):
static String getExtension(String str){
int begin = str.lastIndexOf(".");
if(begin == -1)
return null;
String extension = str.substring(begin + 1);
return extension;
}
Check if the extension is valid: 检查扩展名是否有效:
static boolean isExtensionValid(String extension){
String validExtensions [] = new String[]{
"json", "txt"
};
for(String str : validExtensions )
if(str.equals(extension))
return true;
return false;
}
Test the whole thing: 测试整个事情:
public static void main (String[] args){
String val = "Kappa.txt";
String extension = getExtension(val);
if(extension != null){
System.out.println(extension);
System.out.println(isExtensionValid(extension));
} else {
//Handle no extension found here
}
}
if you want to get any extension after .
如果你想在之后得到任何扩展
.
you can use String split here some examples 你可以在这里使用String split一些例子
String exts[] = filename.split("\\.");
int extIndex = exts.length;\\find array length incase user inputed more than one dot
System.out.println(exts[extIndex - 1]); \\ here you get the extension
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.