0

我正在从 Client 类中读取一个文件,并将其传递给 Service 构造函数。我正在使用 StringTockenizer 解析传递的 1 行字符串。有效行中应该正好有 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

任何帮助将不胜感激。

4

1 回答 1

2

您的解析器方法不会调用token.hasMoreTokens()以实际检查下一个令牌是否存在 - 在读取第五个字段后可能想要这样做。

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

于 2013-11-02T13:17:50.937 回答