Filenotfound返回捕获

时间:2015-11-04 21:27:44

标签: java

我希望能够让我的catch请求用户输入文件名,直到文件名有效,是否有人能够给我一个如何安排我的代码来执行此操作的建议?

import java.io.File;

import java.io.FileNotFoundException; import java.util.Scanner;

public class Requierment2 {     public static void main(String [] 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();
    }

}

}

2 个答案:

答案 0 :(得分:0)

  1. 使用规则打印所需文件名类型的更全面示例。
  2. 使用'visitor'模式实现例外规则。

答案 1 :(得分: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();
    }
}