在构造函数中尝试/捕获不正确的变量

时间:2013-11-02 13:10:01

标签: java exception-handling constructor

我正在从Client类读取文件,并将其传递给Service构造函数。我正在使用StringTockenizer来解析传递的1行String。有效行应该包含5个项目。如果该行包含多于或少于5个项目,则应将其视为无效并忽略。

如果构造函数抛出异常,我希望稍后在Client类中处理它。 我遇到的问题是我无法弄清楚如何正确读取文件,忽略无效行,然后继续读取直到文件结束。有人能指出我正确的方向吗?

 import java.util.StringTokenizer;   //allows use of StringTockenizer
 import java.util.ArrayList;   //allows use of ArrayList
 import java.io.Serializable;   //allows use of Serializable

 public class MailingLabel implements Serializable
 {   //start class
   public String name, address, city, state, zipCode;

  public MailingLabel(String input) throws IllegalMailingLabelException  
      //accepts String parameter from Client
  {   //start constructor
  int count = 0;
  String extra;   //used to hold extra tokens from invalid lines
  StringTokenizer token = new StringTokenizer(info, ",");

  name = token.nextToken();   //1st token in string
  count++;
  address = token.nextToken();   //2nd token in string
  count++;
  city = token.nextToken();   //3rd token in string
  count++;
  state = token.nextToken();   //4th token in string
  count++;
  zipCode = token.nextToken();   //5th token in string
  count++;   //count should equal 5
   extra = token.nextToken();   //holds extra tokens
   count++;

  if (count > 5)
     throw new IllegalMailingLabelException("Exactly 5 fields are required");

如果数据行中有6个令牌,则会成功抛出错误,但现在它会为有效行引发错误(因为该行不包含第6个令牌)。我不想为此写另一个例外。                } //结束构造函数

下面的

是我构造的异常

  public class IllegalMailingLabelException extends NoSuchFieldException 
 {   //start class
 public IllegalMailingLabelException(String message)
 {   //start constructor
  super( message );
  }   //end constructor
  }   //end class

//下面是客户端

 import java.io.*;   //allows use of BufferedReader & ObjectOutputStream
 import java.util.*;   //allows use of ArrayList

 public class Client1
 {   //start class

public static void main(String[] args) throws IOException
{   //start main  
  String input;   //holds each line read from file

  try    
  {   //start try, opens and reads the file       
     BufferedReader bufIn = new BufferedReader(new FileReader(new File("testData.txt")));

testData.txt中的示例数据是: 1,2,3,4,5,6 //这应该是无效数据 1,2,3,4 //这也应该是无效数据 1,2,3,4,5 //这是有效的数据

     System.out.println("Beginning to read file");

     while ( (info = bufIn.readLine()) != null)
     {   //start while
        try
        {   //start try to create new MailingLabel Object
           MailingLabel label = new MailingLabel(input);                  
        }  //end try
        catch(IllegalMailingLabelException e)
       {   //start catch, if object not created 
                    //(handles the exception thrown by service)
           System.out.println(e.getMessage());  
                        //prints error message from service constructor
        }   //end catch

     }   //end while
     System.out.println("Finshed reading file");
     bufIn.close();   //closes the file
  }   //end try

  catch (FileNotFoundException e)   //if file isn't found
  {   //start catch
     System.out.println("File not found");
  }   //end catch
 }   //end main
 }   //end class

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

您的解析器方法不会调用token.hasMoreTokens()来实际检查下一个令牌是否存在 - 可能希望在读取第五个字段后执行此操作。

或者,只需调用input.split(",")并将字段作为数组获取 - 然后验证字段数就很简单。