2015-04-06 67 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

相关问题