simple conditional in java (unexpected issue)

前端 未结 3 1567
不知归路
不知归路 2021-01-29 14:52

I have an unexpected issue when using a conditional operator in java. The code is supposed to check if the ArrayList contains a specific string, then it returns tru

3条回答
  •  无人共我
    2021-01-29 15:30

    Try this:

    public boolean isDone() {
    
        ArrayList al = new ArrayList();
    
        al.add(d1.getText());
        al.add(d2.getText());   
        al.add(d3.getText());    
        al.add(d4.getText());   
        al.add(d5.getText()); 
    
        for (String str : al)
            if (str != null && str.contains("."))
                return false;
    
        return true;
    
    }
    

    You have to check each string individually, the contains() method in ArrayList will return true only if the exact string "." is present in the list, not if one of the strings in the list contains a dot.

提交回复
热议问题