[英]If I am asking a user to input a file, and the file does not exist, how can I continue to ask for the file name without the program stopping?
This is what I have... I know it should require a loop or perhaps the.empty() method from the File class, but I'm not sure.. any help is appreciated.这就是我所拥有的......我知道它应该需要一个循环,或者可能需要文件 class 中的.empty() 方法,但我不确定.. 任何帮助表示赞赏。 What I have will open a file, read from the file on each line, and then return back the amount of characters in the file, the amount of words in the file, and the number of sentences in the file.我所拥有的将打开一个文件,从文件中读取每一行,然后返回文件中的字符数、文件中的单词数以及文件中的句子数。
public class FileExample{
public static void main(String[] args) throws FileNotFoundException, IOException{
Scanner in = new Scanner(System.in);
boolean fileFound = false;
try{
System.out.println("What is the name of the file?");
inputFile = in.nextLine();
File file = new File(inputFile);
fileFound = file.exists();
FileInputStream fileStream = new FileInputStream(file);
InputStreamReader input = new InputStreamReader(fileStream);
BufferedReader reader = new BufferedReader(input);
if(!file.exists()){
}
while((line = reader.readLine()) != null){
if(!(line.equals(""))){
...
}
}
}
catch(FileNotFoundException e){
System.out.println("File not found.");
}
System.out.println("output data");
}
}
You need to make a while loop and move the try block inside the loop.您需要创建一个 while 循环并将 try 块移动到循环内。
while(true){
try{
System.out.println("What is the name of the file?");
inputFile = in.nextLine();
File file = new File(inputFile);
if(!file.exists()){
continue;
}
FileInputStream fileStream = new FileInputStream(file);
InputStreamReader input = new InputStreamReader(fileStream);
BufferedReader reader = new BufferedReader(input);
while((line = reader.readLine()) != null){
if(!(line.equals(""))){
characterCount += line.length();
String[] wordList = line.split("\\s+");
countWord += wordList.length;
String[] sentenseList = line.split("[!?.:]+");
sentenseCount += sentenseList.length;
}
}
break;
}
catch(FileNotFoundException e){
System.out.println("File not found.");
}
}
Use a while loop, until the file exists.使用 while 循环,直到文件存在。 Example:例子:
...
System.out.println("What is the name of the file?");
inputFile = in.nextLine();
File file = new File(inputFile);
fileFound = file.exists();
while(!fileFound){
System.out.println("The file does not exist. What is the name of the file?");
inputFile = in.nextLine();
file = new File(inputFile);
fileFound = file.exists();
}
FileInputStream fileStream = new FileInputStream(file);
InputStreamReader input = new InputStreamReader(fileStream);
BufferedReader reader = new BufferedReader(input);
...
Add a do-while to your code.在您的代码中添加一个 do-while。 Like this.像这样。 not exactly sure if that's gonna run but i hope you get the idea不完全确定这是否会运行,但我希望你能明白
do {
try {
System.out.println("What is the name of the file?");
inputFile = in.nextLine();
File file = new File(inputFile);
fileFound = file.exists();
FileInputStream fileStream = new FileInputStream(file);
InputStreamReader input = new InputStreamReader(fileStream);
BufferedReader reader = new BufferedReader(input);
while((line = reader.readLine()) != null){
if(!(line.equals(""))){
characterCount += line.length();
String[] wordList = line.split("\\s+");
countWord += wordList.length;
String[] sentenseList = line.split("[!?.:]+");
sentenseCount += sentenseList.length;
}
}
} catch (FileNotFoundException e){
System.out.println("File not found.");
}
} while (!fileFound) {
System.out.println("Character count: "+characterCount);
System.out.println("Word count: "+countWord);
System.out.println("Sentence count: "+sentenseCount);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.