【发布时间】:2012-08-27 08:13:22
【问题描述】:
如何循环 try/catch 语句?我正在制作一个使用扫描仪读取文件的程序,它正在从键盘读取它。所以我想要的是如果文件不存在,程序会说“这个文件不存在,请重试。”然后让用户输入不同的文件名。我尝试了几种不同的方法来尝试这样做,但是我所有的尝试都以程序崩溃告终。
这就是我所拥有的
try {
System.out.println("Please enter the name of the file: ");
Scanner in = new Scanner(System.in);
File file = new File(in.next());
Scanner scan = new Scanner(file);
} catch (Exception e) {
e.printStackTrace();
System.out.println("File does not exist please try again. ");
}
【问题讨论】:
-
嗯,你需要实际使用
for或while(或其他 - 在学究之前 ;-) 才能循环。