我希望能夠讓我的catch請求用戶輸入文件的名稱,直到文件名有效,任何人都可以給我一個關於如何安排我的代碼的建議嗎?Filenotfound return catch
import java.io.File;
import java.io.FileNotFoundException; import java.util.Scanner;
公共類Requierment2 { 公共靜態無效的主要(字串[] args){
String filename ="";
Scanner keyboard = new Scanner(System.in);
System.out.println ("Press K for keyboard or F to read expressions from a file");
String user_input=keyboard.nextLine();
String [] ui = user_input.split(" ");
if (ui[0].equals("k") | ui[0].equals("K")) {
while (true){
System.out.println("Please Enter a Post-Fix Expression (eg: 5 2 *)");
String postfix=keyboard.nextLine();
String [] elements =postfix.split(" ");
if (postfix.equals("")){
System.out.println("Application Closed");
keyboard.close();
System.exit(0);
}
if (elements.length >=3){
try{
float num1, num2;
num1 = Float.parseFloat(elements[0]);
num2 = Float.parseFloat(elements[1]);
if(elements[2].equals("+")){
System.out.println("Total: "+(num1 + num2));
}
else if(elements[2].equals("*")){
System.out.println("Total: "+(num1 * num2));
}
else if(elements[2].equals("/")){
System.out.println("Total: "+(num1/num2));
}
else if(elements[2].equals("-")){
System.out.println("Total: "+(num1 - num2));
}
else{
System.out.println("Error Invalid Expression: "+ postfix);
}
}
catch(NumberFormatException e){
System.out.println("Error Invalid Expresion: "+postfix);
}
}
else if (elements.length <3) {
System.out.println("Error Invalid Expression: "+ postfix);
}
}
}
else if (ui[0].equals("f") | ui[0].equals("F"))
{
try{
System.out.println("Please enter file name eg: Demo.txt");
filename= keyboard.nextLine();
Scanner s = new Scanner(new File(filename));
System.out.println("Processing "+ filename);
System.out.println();
String line= s.nextLine();
String array []= line.split(" ");
if (array.length >=3){
try{
float array1, array2, total1, total2, total3, total4, array3 , array5;
array1 = Float.parseFloat(array[0]);
array2 = Float.parseFloat(array[1]);
total1 = array1 + array2;
total2 = array1 * array2;
total3 = array1/array2;
total4 = array1 - array2;
if(array[2].equals("+")){
System.out.println("Postfix Expression: " + line);
System.out.println(array1 + " " + array[2] + " " + array2 + " = " + total1);
System.out.println();
}
else if(array[2].equals("*")){
System.out.println("Postfix Expression: " + line);
System.out.println(array1 + " " + array[2] + " " + array2 + " = " + total2);
System.out.println();
}
else if(array[2].equals("/")){
System.out.println("Postfix Expression: " + line);
System.out.println(array1 + " " + array[2] + " " + array2 + " = " + total3);
System.out.println();
}
else if(array[2].equals("-")){
System.out.println("Postfix Expression: " + line);
System.out.println(array1 + " " + array[2]+ " " + array2 + " = " + total4);
System.out.println();
}
else{
System.out.println(" Error Invalid Expression: "+ line);
System.out.println();
}
}
catch(NumberFormatException e){
System.out.println(" Error Invalid Expresion: "+ line);
System.out.println();
}
}
else if (array.length <3) {
System.out.println(" Error Invalid Expression: "+ line);
System.out.println();
}
while (s.hasNext()) {
String nl= s.nextLine();
String ar []= nl.split(" ");
if (ar.length >=3){
try{
float ar1, ar2, total1, total2, total3, total4;
ar1 = Float.parseFloat(ar[0]);
ar2 = Float.parseFloat(ar[1]);
total1 = ar1 + ar2;
total2 = ar1 * ar2;
total3 = ar1/ar2;
total4 = ar1 - ar2;
if(ar[2].equals("+")){
System.out.println("Postfix Expression: " + nl);
System.out.println(ar1 + " " + ar[2] + " " + ar2 + " = " + total1);
System.out.println();
}
else if(ar[2].equals("*")){
System.out.println("Postfix Expression: " + nl);
System.out.println(ar1 + " " + ar[2] + " " + ar2 + " = " + total2);
System.out.println();
}
else if(ar[2].equals("/")){
System.out.println("Postfix Expression: " + nl);
System.out.println(ar1 + " " + ar[2] + " " + ar2 + " = " + total3);
System.out.println();
}
else if(ar[2].equals("-")){
System.out.println("Postfix Expression: " + nl);
System.out.println(ar1 + " " + ar[2]+ " " + ar2 + " = " + total4);
System.out.println();
}
else{
System.out.println(" Error Invalid Expression: "+ nl);
System.out.println();
}
}
catch(NumberFormatException e){
System.out.println(" Error Invalid Expresion: "+ nl);
System.out.println();
}
}
else if (array.length <3) {
System.out.println(" Error Invalid Expression: "+ nl);
System.out.println();
}
}
}
catch (FileNotFoundException e){
System.out.println("Error: That file does not exist, please re-enter: ");
filename= keyboard.nextLine();
Scanner s ;
}
}
else{
System.out.println("Neither K or F have been entered");
System.out.println("System Terminated");
keyboard.close();
}
}
}
請代碼減少到相關的部分。 – Turing85
歡迎來到StackOverflow。請閱讀並遵守幫助文檔中的發佈準則。 [MCVE](http://stackoverflow.com/help/mcve)適用於此處。 – Prune