Java try/catch在构造函数中查找不正确的变量量
我正在从客户机类读取一个文件,并将其传递给服务构造函数。我正在使用StringTockenizer解析传递的字符串的1行。有效行中应正好包含5项。如果该行包含的项目多于或少于5项,则应将其视为无效并忽略 如果构造函数抛出异常,我希望稍后在客户机类中处理它。 我遇到的问题是,我不知道如何正确读取文件,忽略无效行,然后继续读取直到文件结束。有人能给我指出正确的方向吗Java try/catch在构造函数中查找不正确的变量量,java,exception-handling,constructor,Java,Exception Handling,Constructor,我正在从客户机类读取一个文件,并将其传递给服务构造函数。我正在使用StringTockenizer解析传递的字符串的1行。有效行中应正好包含5项。如果该行包含的项目多于或少于5项,则应将其视为无效并忽略 如果构造函数抛出异常,我希望稍后在客户机类中处理它。 我遇到的问题是,我不知道如何正确读取文件,忽略无效行,然后继续读取直到文件结束。有人能给我指出正确的方向吗 import java.util.StringTokenizer; //allows use of StringTockeniz
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
任何帮助都将不胜感激 您的解析器方法不会调用实际检查下一个标记是否存在-可能需要在读取第五个字段后执行此操作
或者,只需调用并获取字段作为数组,然后验证字段的数量就很简单了。读取五个标记,然后通过
hasMoreTokens检查。