如何添加do while循环,以便在遇到FileNotFoundException类时,通知用户该文件不存在,并为用户提供输入另一个文件名的机会?到目前为止,这就是我所拥有的。谢谢
do
{
// Get a file name from the user.
fileName = JOptionPane.showInputDialog("Enter " +
"the name of a file:");
// Attempt to open the file.
try
{
file = new File(fileName);
inputFile = new Scanner(file);
JOptionPane.showMessageDialog(null,
"The file was found.");
}
catch (FileNotFoundException e)
{
JOptionPane.showMessageDialog(null,
"File not found.");
}
///// while(flag+f)
JOptionPane.showMessageDialog(null, "Done.");
System.exit(0);
}
}
答案 0 :(得分:0)
尝试类似这样的操作...添加一个标记“确定”,然后执行操作,直到变为真为止。比停止循环。
boolean ok = false;
do
{
// Get a file name from the user.
fileName = JOptionPane.showInputDialog("Enter " +
"the name of a file:");
// Attempt to open the file.
try
{
file = new File(fileName);
inputFile = new Scanner(file);
JOptionPane.showMessageDialog(null,
"The file was found.");
ok = true;
}
catch (FileNotFoundException e)
{
JOptionPane.showMessageDialog(null,
"File not found.");
}
while(ok == false);}
JOptionPane.showMessageDialog(null, "Done.");
System.exit(0);
}