2015-06-29 153 views
0

我需要編寫一個程序來讀取文本文件並計算不同的東西,但是,如果未找到文件名,它應該打印一條錯誤消息,其中包含以下錯誤消息catch塊:在Java中嘗試並捕獲錯誤

java.io.FileNotFoundException: inputValues (The system cannot find the file specfied) 
    ....... 

不過,我反而收到此錯誤信息:

Exception in thread "main" java.util.InputMismatchException 
at java.util.Scanner.throwFor(Unknown Source) 
at java.util.Scanner.next(Unknown Source) 
at java.util.Scanner.nextInt(Unknown Source) 
at java.util.Scanner.nextInt(Unknown Source) 
at Project6.main(Project.java:50) 

這裏是我的代碼部分:

Scanner console = new Scanner(System.in);     
     System.out.print("Please enter the name of the input file: ");        // Prompts User to Enter Input File Name 
     String inputFileName = console.nextLine();             // Reads Input File Name 

     Scanner in=null;                   // 

     try 
      { 
       in = new Scanner(new File(inputFileName));           // Construct a Scanner Object 
      } 
     catch (IOException e)                  // Exception was Thrown 
      { 
       System.out.print("FileNotFound Exception was caught, the program will exit.");  // Error Message Printed because of Exception 
       e.printStackTrace(); 
      } 

     int n = in.nextInt();                  // Reads Number of Line in Data Set from First Line of Text Document 
     double[] array = new double[n];                // Declares Array with n Rows 

第50行是:int n = in.nextInt();

除了打印不正確的錯誤消息,我的程序運行得很好。

任何/所有的幫助將不勝感激!

+1

文件被發現,但文件的內容是不是int –

+0

那麼,如何改變這種只打印FileNotFound異常? – coscdummy

+0

從方法返回,退出程序,或以其他方式處理下'in'爲null時。如果操作失敗,則無法獲取下一個號碼。 – user2864740

回答

0

您的異常拋出行in.nextInt()你試圖讀取整數,但掃描儀發現其他東西。如果你需要把它們全部作爲一個單一的錯誤,你可以把它們放在同一個try catch塊中,如下所示。

Scanner in=null;                   // 

    try 
    { 
     in = new Scanner(new File(inputFileName)); 
     // Construct a Scanner Object 
     int n = in.nextInt();                   

     // Reads Number of Line in Data Set from First Line of Text Document 
     double[] array = new double[n]; 
    } catch (IOException e)                   
    // Exception was Thrown 
    { 
     System.out.print("FileNotFound Exception was caught, the program will exit.");  
     // Error Message Printed because of Exception 
     e.printStackTrace(); 
    } catch (InputMismatchException e)                   
    // Exception was Thrown 
    { 
     System.out.print("Integer not found at the beginning of the file, the program will exit.");  
     // Error Message Printed because of Exception 
     e.printStackTrace(); 
    } 
0

醜陋的格式錯誤的代碼很難閱讀和理解。這是你遇到麻煩的原因之一。

這更簡單:從此開始。

package cruft; 

import java.io.BufferedReader; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 
import java.util.List; 

/** 
* MessyFileDemo 
* @author Michael 
* @link https://stackoverflow.com/questions/31106118/try-and-catch-error-in-java 
* @since 6/28/2015 8:20 PM 
*/ 
public class MessyFileDemo { 

    public static void main(String[] args) { 
     List<Double> values; 
     InputStream is = null; 
     try { 
      String inputFilePath = args[0]; 
      is = new FileInputStream(inputFilePath); 
      values = readValues(is); 
      System.out.println(values); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      close(is); 
     } 
    } 

    private static void close(InputStream is) { 
     try { 
      if (is != null) { 
       is.close(); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    public static List<Double> readValues(InputStream is) throws IOException { 
     List<Double> values = new ArrayList<>(); 
     if (is != null) { 
      BufferedReader br = new BufferedReader(new InputStreamReader(is)); 
      String line; 
      while ((line = br.readLine()) != null) { 
       String [] tokens = line.split(","); 
       for (String token : tokens) { 
        values.add(Double.parseDouble(token)); 
       } 
      } 
     } 
     return values; 
    } 

}