【问题标题】:Validation of object in array in javajava中数组中对象的验证
【发布时间】:2019-11-28 15:20:34
【问题描述】:

如何使用另一个类中的对象验证一个类中的数组。 我正在为一个钓鱼比赛做项目,我有一个类别允许的物种,但必须在另一个类别中进行捕获。

#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

不确定需要多少帮助我的问题与布尔 checkSpecies 相关。我如何将它与 Fish 类中的 speciesAllowed 进行比较。因此,这些是用户可以输入的唯一 Fish。

不,这不是整个程序,这只是我需要帮助的 sn-ps。谢谢。

【问题讨论】:

    标签: java string validation arraylist


    【解决方案1】:

    如果我理解正确的话,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;
    }
    

    无需遍历input。我假设input 是鱼,必须对其进行验证。相反,您可以检查input 是否存在于允许列表中,例如:

    return ALLOWED_SPECIES_LIST.contains(input.toLowerCase())
    

    ALLOWED_SPECIES_LIST 可能类似于
    Arrays.asList(allowed1,allowed2,allowed3,allowed4) 如果你想使用定义的Fist,已经有fishList我们可以使用:

    return fishList.stream().map(Fish::speciesAllowed).anyMatch(input);
    

    或没有流:

    for(String fish: fishList) {
      if (fish.speciesAllowed.equals(input)){
         return true; 
      }
    }
    return false; 
    }
    

    我希望这会有所帮助。

    【讨论】:

    • 感谢您的帮助。我现在确实有了更好的理解,这肯定有帮助,但我仍然没有完全理解。我添加到 fishList 的鱼是允许的物种。锦标赛类将接受用户输入,我需要确保他们输入的鱼的种类等于 fishList 中的一种。希望这会澄清。我在 checkSpecies 中做了 allowed1-4 作为让它工作的一种方式,所以我知道它可以完全运行
    • 这是我声明的arrayList,如果这有帮助的话。静态 ArrayList fishList = new ArrayList(); static ArrayList fishermanList = new ArrayList(); static ArrayList tournamentList = new ArrayList();
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-22
    • 2022-11-25
    • 1970-01-01
    • 2015-10-24
    • 2021-05-18
    • 2021-08-21
    • 1970-01-01
    相关资源
    最近更新 更多