我正在处理一个有 5 个类的项目。其中之一是验证类。我在对照类中验证了数据,但教授希望在单独的类中使用它。这是我的代码示例。用户在令牌中输入信息。运行良好,但在控制器类中,我应该"调用"具有自己的类的验证。我应该如何编码?
while (tkCustomer.hasMoreTokens()){
//store each token in the corresponding variable
//Make sure we format integers
//variablename = tkCustomer.nextToken();
firstName = tkCustomer.nextToken( );
lastName = tkCustomer.nextToken ( );
phone = tkCustomer.nextToken ( );
nbrVehicle = Integer.parseInt(tkCustomer.nextToken( ));
nbrTanks = Integer.parseInt(tkCustomer.nextToken( ));
}
//validate each data entered here
int firstNameLength = firstName.length();
if(firstNameLength == 0){
String errorMessage = "Please Enter a Valid First Name";
JOptionPane.showMessageDialog(null, errorMessage, "Invalid Name", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}else{
int lastNameLength = lastName.length();
if(lastNameLength == 0){
String errorMesssage = "Please Enter a Valid Last Name";
JOptionPane.showMessageDialog(null, errorMesssage, "Invalid Name", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}else{
int phoneLength = phone.length();
if(phoneLength != 10) {
String errorMesssage = "Please Enter a Valid Phone Number";
JOptionPane.showMessageDialog(null, errorMesssage, "Invalid Phone Number", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}else{
if(nbrVehicle < 1 || nbrVehicle > 10) {
String errorMesssage = "Please Enter a Number Between 1 & 10 for Your Order of Vehicles";
JOptionPane.showMessageDialog(null, errorMesssage, "Invalid Vehicle Order", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}else{
if(nbrTanks != 2 && nbrTanks != 4 && nbrTanks != 8 && nbrTanks != 10 && nbrTanks != 15 && nbrTanks != 20){
String errorMesssage = "Please Enter Either 2, 4, 8, 10, 15, or 20 for Your Order of Tanks";
JOptionPane.showMessageDialog(null, errorMesssage, "Invalid Number of Tanks", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
}
}
}
但是,一旦用户选择了提供给他们的某个选项,程序就会崩溃。 下面是控件类的完整代码。DU第一轮分词器项目程序运行良好。第二轮后,程序崩溃,给我这个错误:
线程"main"java.util.NoSuchElementException 中的异常 at java.util.StringTokenizer.nextToken(StringTokenizer.java:349( 在项目3示例中。HayloFactoryController.main(HayloFactoryController.java:126(
我已经尝试了"虽然有更多的令牌"方法,但这只会将程序发送到一个无限循环中,并且只能通过停止按钮终止。
下面是控件类的完整代码:
package project3example;
import java.util.StringTokenizer;
import javax.swing.JOptionPane;
public class HayloFactoryController {
/**
* @param args
*/
public static void main(String[] args) {
//create variables to hold information collected from the user
String firstName = null;
String lastName = null;
String phone = "";
int nbrTanks = 0;
int nbrVehicle = 0;
double total = 0;
do{
//collect the data entered by the user in
//variables
String customerMessage = "Please enter the following separated by spaces"
+ "nn"
+ "- Customer First Namen"
+ "- Customer Last Namen"
+ "- Customer Phonen"
+ "- Number of Vehiclesn"
+ "- Number of Tanks"
+ "nn"
+ "Example: Homer Simpson 9094559384 5 8nn"
;
StringTokenizer tkCustomer = new StringTokenizer(
JOptionPane.showInputDialog(customerMessage));
while (tkCustomer.hasMoreTokens()){
//store each token in the corresponding variable
//Make sure we format integers
//variablename = tkCustomer.nextToken();
firstName = tkCustomer.nextToken( );
lastName = tkCustomer.nextToken ( );
phone = tkCustomer.nextToken ( );
nbrVehicle = Integer.parseInt(tkCustomer.nextToken( ));
nbrTanks = Integer.parseInt(tkCustomer.nextToken( ));
}
//validate each data entered here
int firstNameLength = firstName.length();
if(firstNameLength == 0){
String errorMessage = "Please Enter a Valid First Name";
JOptionPane.showMessageDialog(null, errorMessage, "Invalid Name", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}else{
int lastNameLength = lastName.length();
if(lastNameLength == 0){
String errorMesssage = "Please Enter a Valid Last Name";
JOptionPane.showMessageDialog(null, errorMesssage, "Invalid Name", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}else{
int phoneLength = phone.length();
if(phoneLength != 10) {
String errorMesssage = "Please Enter a Valid Phone Number";
JOptionPane.showMessageDialog(null, errorMesssage, "Invalid Phone Number", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}else{
if(nbrVehicle < 1 || nbrVehicle > 10) {
String errorMesssage = "Please Enter a Number Between 1 & 10 for Your Order of Vehicles";
JOptionPane.showMessageDialog(null, errorMesssage, "Invalid Vehicle Order", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}else{
if(nbrTanks != 2 && nbrTanks != 4 && nbrTanks != 8 && nbrTanks != 10 && nbrTanks != 15 && nbrTanks != 20){
String errorMesssage = "Please Enter Either 2, 4, 8, 10, 15, or 20 for Your Order of Tanks";
JOptionPane.showMessageDialog(null, errorMesssage, "Invalid Number of Tanks", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
}
}
}
//Create a customer Object
//CustomerObject variableName = new CustomerObject(arguments)
HayloCustomer customer = new HayloCustomer (firstName, lastName, phone, nbrVehicle, nbrTanks, total);
//Present the customer with a choice of vehicles
String[] choices = {"EV-EX 4", "EV-EX 6", "EV-DX 9", "EV-DX 12", "EV-SX 13"};
int response = JOptionPane.showOptionDialog(
null // center over parent
, "Select a Vehicle type and Number of Fuel Cells" // message
, "Vehicle & Fuel Cell Selection" // title in title bar
, JOptionPane.YES_NO_OPTION // Option type
, JOptionPane.PLAIN_MESSAGE // messageType
, null // icon
, choices // Options
, "APS 24" // initial value
);
//get the selection from the customer
StringTokenizer tkVehicle= new StringTokenizer(choices[response]);
//populate the vehicle variables
String vehicleType;
int nbrCells;
int costVehicle;
int tankCost;
//while (tkVehicle.hasMoreTokens());
vehicleType = tkVehicle.nextToken();
nbrCells = Integer.parseInt(tkVehicle.nextToken());
costVehicle = Integer.parseInt(tkVehicle.nextToken());
tankCost = Integer.parseInt(tkVehicle.nextToken());
//Create our vehicle object
//VehicleObject variableName = new VehicleObject(arguments)
HayloVehicle vehicleFactory = new HayloVehicle(vehicleType, nbrCells, costVehicle, tankCost);
//Create our factory object
//FactoryObject factoryVariableName = new FactoryObject(customerObjectVariable, vehicleObjectVariable);
HayloFactory factory = new HayloFactory(customer, vehicleFactory);
//ask the object to process the order
//factoryVariableName.process();
customer.toString();
vehicleFactory.toString();
factory.process();
factory.toString();
//write code below to display the result for each order
JOptionPane.showMessageDialog(null, factory.getSummary());
}while (JOptionPane.showConfirmDialog(null, "Enter More Orders?") == JOptionPane.YES_OPTION);
//write code below to display the summary for all the orders
JOptionPane.showMessageDialog(null, HayloFactory.salesSummary());
}
}
任何帮助将不胜感激!初学者在这里,成绩在线!
最简单的方法是在方法中写出一个具有验证逻辑的类,该方法将客户对象作为其参数(从字符串分词器填充字段(并将其传递给该方法。验证可以在方法中完成,并指定一些错误代码/成功代码以从验证返回,并显示错误或成功(如果适用(。还有许多其他设计方法,但这可能是最快的。