java中数组中对象的验证
如何使用另一个类中的对象验证一个类中的数组。 我正在为一个钓鱼比赛做一个项目,我有一个班级允许的鱼种,但必须在另一个班级做捕捞java中数组中对象的验证,java,string,validation,arraylist,Java,String,Validation,Arraylist,如何使用另一个类中的对象验证一个类中的数组。 我正在为一个钓鱼比赛做一个项目,我有一个班级允许的鱼种,但必须在另一个班级做捕捞 #MAIN CLASS# Fish bass = new Fish("Bass", 14, 20); Fish catfish = new Fish("Catfish", 12, 30); Fish trout = new Fish("Trout", 15, 12); Fish walle
#MAIN CLASS#
Fish bass = new Fish("Bass", 14, 20);
Fish catfish = new Fish("Catfish", 12, 30);
Fish trout = new Fish("Trout", 15, 12);
Fish walleye = new Fish("Walleye", 10, 15);
fishList.add(bass);
fishList.add(catfish);
fishList.add(trout);
fishList.add(walleye);
##class Fish##
{
//attributes:
String speciesAllowed;
double sizeLimit;
int bagLimit;
//constructors:
Fish()
{
speciesAllowed = " ";
sizeLimit = 0.0;
bagLimit = 0;
}// end constructor w/o argument
Fish(String speciesAllowed, double sizeLimit, int bagLimit)
{
this.speciesAllowed = speciesAllowed;
this.sizeLimit = sizeLimit;
this.bagLimit = bagLimit;
}//end constructor w/ argument
###class Tournament extends Fish###
{
//attributes:
String ssn;
String species;
double size;
int bag;
double weight;
//------------------------------------------------------------------------
//constructors
Tournament()
{
super();
ssn = " ";
species = " ";
size = 0.0;
bag = 0;
weight = 0.0;
}//end constructor w/0 argument
Tournament(String speciesAllowed, double sizeLimit, int bagLimit, String ssn, String species, double
size, int bag, double weight)
{
super(speciesAllowed, sizeLimit, bagLimit);
this.ssn = ssn;
this.species = species;
this.size = size;
this.bag = bag;
this.weight = weight;
}//end constructor w/ argument
//-------------------------------------------------------------------------
void getCatch()
{
String input = " ";
boolean valid = false;
input = JOptionPane.showInputDialog("Enter SSN: ");
valid = checkSSN(input);
while(!valid)
{
input = JOptionPane.showInputDialog("Invalid SSN...Please re-enter SSN: ");
valid = checkSSN(input);
}
ssn = input;
input = JOptionPane.showInputDialog("Enter species of the catch: ");
//valid = checkSpecies(input);
while(!valid)
{
input = JOptionPane.showInputDialog("Sorry that fish is not allowed ");
//valid = checkSpecies(input);
}
species = input;
//engHours = Integer.parseInt(input);
}//end getBOAT()
//-------------------------------------------------------------------------
void dispTournament()
{
JOptionPane.showMessageDialog(null, "SSN : " + ssn + "\n" +
"Species : " + species + "\n" +
"Size : " + size + "\n" +
"Bag : " + bag + "\n" +
"Weight : " + weight + "\n");
}//end dispTournament
//-------------------------------------------------------------------------
boolean checkSSN(String input)
{
if(input.length() != 9 )
return false;
else
for(int i = 0; i < input.length(); i++)
{
if(!Character.isDigit(input.charAt(i)))
return false;
}
return true;
boolean checkSpecies(String input)
{
String allowed1 = "bass";
String allowed2 = "trout";
String allowed3 = "walleye";
String allowed4 = "catfish";
for(int i = 0; i < input.length(); i++)
{
if(input(i).equalsignorecase(<Fish>speciesAllowed))
return false;
}
return true;
}//end checkSpecies
#主类#
鱼鲈鱼=新鱼(“鲈鱼”,14,20);
鱼鲶鱼=新鱼(“鲶鱼”,12,30);
鱼鳟鱼=新鱼(“鳟鱼”,15,12);
鱼墙眼=新鱼(“墙眼”,10,15);
鱼列表。添加(低音);
鱼列表。添加(鲶鱼);
鱼表。添加(鳟鱼);
添加(walleye);
##分类鱼##
{
//属性:
允许使用字符串类型;
双尺寸限值;
int bagLimit;
//建造商:
鱼()
{
speciesAllowed=“”;
sizeLimit=0.0;
bagLimit=0;
}//结束构造函数,不带参数
鱼(允许使用字符串规格、双尺寸限制、整数限制)
{
this.speciesAllowed=speciesAllowed;
this.sizeLimit=sizeLimit;
this.bagLimit=bagLimit;
}//带参数的结束构造函数
###班级比赛延长了鱼的寿命###
{
//属性:
字符串ssn;
弦种;
双倍大小;
int包;
双倍重量;
//------------------------------------------------------------------------
//建设者
锦标赛()
{
超级();
ssn=“”;
物种=”;
尺寸=0.0;
bag=0;
重量=0.0;
}//结束构造函数w/0参数
锦标赛(允许字符串规格、双码限制、整数包限制、字符串ssn、字符串种类、双码
尺寸,内袋,双倍重量)
{
超级(允许的规格、尺寸限制、袋限);
这个.ssn=ssn;
这个物种=物种;
这个。大小=大小;
这个袋子=袋子;
重量=重量;
}//带参数的结束构造函数
//-------------------------------------------------------------------------
void getCatch()
{
字符串输入=”;
布尔有效=假;
input=JOptionPane.showInputDialog(“输入SSN:”);
有效=检查SSN(输入);
while(!valid)
{
input=JOptionPane.showInputDialog(“无效的SSN…请重新输入SSN:”);
有效=检查SSN(输入);
}
ssn=输入;
input=JOptionPane.showInputDialog(“输入捕获物的种类:”);
//有效=检查物种(输入);
while(!valid)
{
input=JOptionPane.showInputDialog(“对不起,不允许钓鱼”);
//有效=检查物种(输入);
}
物种=输入;
//engHours=Integer.parseInt(输入);
}//(完)
//-------------------------------------------------------------------------
作废
{
JOptionPane.showMessageDialog(空,“SSN:+SSN+”\n)+
种类:“+种类+”\n+
大小:“+Size+”\n+
行李:“+行李+”\n+
“重量:“+重量+”\n”);
}//比赛结束
//-------------------------------------------------------------------------
布尔校验SSN(字符串输入)
{
如果(input.length()!=9)
返回false;
其他的
对于(int i=0;i
我不确定需要多少帮助。我的问题是布尔checkSpecies。我如何将其与Fish类中允许的物种进行比较。这样,用户就只能输入这些鱼类了
不,这不是整个程序,这只是我需要帮助的一小部分。谢谢。如果我理解正确,只允许有4种可能的
鱼:
Fish bass = new Fish("Bass", 14, 20);
Fish catfish = new Fish("Catfish", 12, 30);
Fish trout = new Fish("Trout", 15, 12);
Fish walleye = new Fish("Walleye", 10, 15);
在checkSpecies
中,有一个用户输入验证
boolean checkSpecies(String input)
{
String allowed1 = "bass";
String allowed2 = "trout";
String allowed3 = "walleye";
String allowed4 = "catfish";
for(int i = 0; i < input.length(); i++)
{
if(input(i).equalsignorecase(<Fish>speciesAllowed))
return false;
}
return true;
}
其中,允许的物种列表
可能类似于
Arrays.asList(allowed1、allowed2、allowed3、allowed4)
如果您想使用已定义的Fist
,我们可能会使用鱼列表
:
return fishList.stream().map(Fish::speciesAllowed).anyMatch(input);
或无流:
for(String fish: fishList) {
if (fish.speciesAllowed.equals(input)){
return true;
}
}
return false;
}
我希望这会有所帮助。谢谢你的帮助。我现在确实有了更好的理解,这肯定会有所帮助,但我仍然没有完全理解。我添加到鱼列表中的鱼是允许的种类。锦标赛类将接受用户输入,我需要确保他们输入的鱼的种类与中的一种相同fishList。希望这会澄清。我在checkSpecies中做了allowed1-4,只是为了让它工作,所以我知道它可以完全运行。这里是我声明的arrayList,如果这有帮助的话。静态arrayList fishList=new arrayList();静态arrayList FishmanList=new arrayList();静态ArrayList tournamentList=新建ArrayList();
for(String fish: fishList) {
if (fish.speciesAllowed.equals(input)){
return true;
}
}
return false;
}