2015-11-04 24 views
0

我希望能夠讓我的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(); 
    } 

} 

}

+1

請代碼減少到相關的部分。 – Turing85

+0

歡迎來到StackOverflow。請閱讀並遵守幫助文檔中的發佈準則。 [MCVE](http://stackoverflow.com/help/mcve)適用於此處。 – Prune

回答

0
  1. 打印您需要的規則文件名的類型更全面的例子。
  2. 使用'訪問者'模式來實現異常規則。
+0

看着代碼,在我看來,OP是一名初學者。因此,我不太確信建議「訪客模式」是非常有用/有意義的。 –

0

我建議不要依賴異常,而是看看File()類的isFile()或exists()和isDirectory()方法。因此,創建一個簡單的循環,以保持循環,直到輸入有效的文件名,或isFile()爲false。

實施例1:

import java.io.File; 
import java.util.Scanner; 

public class Requirement2 { 
    public static void main(String[] args) { 
     String filename =""; 
     Scanner keyboard = new Scanner(System.in); 
     while (true) { 
      System.out.println("Please enter file name eg: Demo.txt"); 
      filename= keyboard.nextLine(); 
      File file = new File(filename); 
      if (!file.isFile()) { 
       System.out.println("File does not exist. Try again."); 
      } 
      else { 
       System.out.println("File exists. Proceeding."); 
       break; 
      } 
     } 
     keyboard.close(); 
    } 
} 

實施例2:

import java.io.File; 
import java.util.Scanner; 

public class Requirement2v2 { 
    public static void main(String[] args) { 
     String filename =""; 
     Scanner keyboard = new Scanner(System.in); 
     System.out.println("Please enter file name eg: Demo.txt"); 
     filename= keyboard.nextLine(); 
     File file = new File(filename); 
     while (!file.isFile()) { 
      System.out.println("File does not exist. Try again."); 
      filename= keyboard.nextLine(); 
      file = new File(filename); 
     } 
     System.out.println("File exists. Proceeding."); 
     keyboard.close(); 
    } 
}