2015-04-06 45 views
0

我有一個從文本文件中讀取信息並以有組織的格式顯示的程序。如果文本文檔的格式不符合預定格式,如何生成錯誤。如何在.txt格式不正確時生成錯誤

這是我到目前爲止有:

package question; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.NoSuchElementException; 
import java.util.Scanner; 
public class question1{ 

public static void displayResults(double dinnerExpense, double lodgingExpense, double conferenceExpense) 
{ 
    System.out.println("Dinner expense: $"+dinnerExpense); 
    System.out.println("Lodging expense: $"+lodgingExpense); 
    System.out.println("Conference expense: $"+conferenceExpense); 
} 
public static void searchAndDisplayFile(String name) 
{ 
    try 
    {   
     Scanner inFile = new Scanner(new File(name));   
     double dinnerExpense=0; 
     double lodgingExpense=0; 
     double conferenceExpense=0; 
     while(inFile.hasNext()) 
{ 
      inFile.useDelimiter("; "); 
      String activity =inFile.next(); 
      if (activity.equals("Dinner")) 
{ 
    dinnerExpense+=(inFile.nextDouble()); 
} 
    if (activity.equals("Lodging")) 
{ 
     lodgingExpense+=(inFile.nextDouble()); 
} 
    if (activity.equals("Conference")) 
{ 
     conferenceExpense+=(inFile.nextDouble()); 
} 
} 
     inFile.close(); 
     displayResults(dinnerExpense, lodgingExpense,conferenceExpense); 
} 
    catch (FileNotFoundException e) 
{ 
     System.out.println("File not found!"); 
} 
} 
public static void main(String[] args) throws FileNotFoundException{ 
    Scanner in = new Scanner (System.in); 
    System.out.println("Please enter the file name: "); 
    String name = in.nextLine(); 
    searchAndDisplayFile(name); 
    System.out.println("Make sure the file exists or is type correctly."); 
} 

}

回答

0

插入

throw new YourExceptionSubtype(...); 

拋出一個異常(如果該異常是沒有的RuntimeException,你必須經過添加throws YourExceptionSubtype參數列表YourExceptionSubtype可能是javaAPI異常(IOException將適合)或您自己實現的異常(必須有Exception作爲基礎類)。

0

您可以使用異常。異常是執行程序期間發生的錯誤。

您可以通過擴展異常類例如創建自己的自定義異常:

public class YourCustomException extends Exception 
{ 
    public TestException(String msg) 
    { 
     super(msg); 
    } 
} 

所以,無論你想在你的應用程序,例如錯誤(狗不能有10個耳)你扔YourCustomException

if(dogEars > 3) 
    throw new YourCustomException("Dogs can't have more than 3 ears"); 

例外的處理方式。你處理異常的方法是使用try/catch塊

try 
{ 
    Dog.dogEars = 5; 
} 
catch(YourCustomException yce) 
{ 
    // handle the exception here 
} 

在您例如,對於Word文檔,如果它沒有被格式化「名稱,類別,金額花」,你可以做這樣的事情:

if (activity.equals("Dinner")) // checking the Category column 
{ 
    dinnerExpense+=(inFile.nextDouble()); 
} 
else 
{ 
    throw new FileFormattingException("The Category should be specified"); 
} 

和被調用displayResults(String name)方法,你應該圍繞代碼用try/catch塊的主要方法:

try 
{ 
    searchAndDisplayFile(name); 
} 
catch(FileFormattingException ffe) 
{ 
    ffe.printStackTrace(); 
} 

創建一個類爲您FileFormattingException異常

public class YourCustomException extends Exception 
{ 
    public TestException(String msg) 
    { 
     super(msg); 
    } 
} 
+0

我該如何去使用這個語法。即如果單詞文檔未格式化爲「名稱;類別;花費的金額」,它會產生錯誤? – kjd82

+0

你可以添加如何格式化文件,以及如何搜索名稱,類別和金額花費? – Arlind

+0

例如「John Doe;晚餐; 146.54; 2014年6月12日」晚餐可以在任何會議或住宿中進行。 – kjd82

相關問題