Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Java字符串与数组匹配时出现问题_Java_Arrays_String - Fatal编程技术网

将Java字符串与数组匹配时出现问题

将Java字符串与数组匹配时出现问题,java,arrays,string,Java,Arrays,String,我需要查看字符串值是否与对象值匹配,但为什么这不起作用 public int countPacks(String flavour) { int counter = 0; for(int index = 0; index < packets.size(); index++) { if (packets.equals(flavour)) { counter ++; } else {

我需要查看字符串值是否与对象值匹配,但为什么这不起作用

public int countPacks(String flavour) {
    int counter = 0;
    for(int index = 0; index < packets.size(); index++) {            
        if (packets.equals(flavour)) {
           counter ++;
        }
        else {
           System.out.println("You have not entered a correct flavour");
        }
    } 
    return counter; 
}
public int countPacks(字符串风格){
int计数器=0;
对于(int index=0;index
你的意思可能是

if (packets.get(index).equals(flavour)) {
   ...
什么是“包”?它不会是一个字符串-那么它怎么会等于一个字符串呢

我猜你的测试是:

if (packets.get(index).equals(flavour))
然而,你仍然要为每一包“错误”的味道打印“你没有输入正确的味道”,即使有一包味道正确


我怀疑您希望将错误报告移动到方法的末尾,在循环之后,并且仅当
计数器==0

您应该这样做,以确保您确实以最简单的方式比较字符串

我也修复了你的循环

public int countPacks(String flavour, List packets)
    {
        int counter = 0;
        for (Iterator iter = packets.iterator(); iter.hasNext();) {
            Map packet = (Map) iter.next();

            if (packet.get("flavour").toString().equalsIgnoreCase(flavour)) {
                counter ++;
            }
          else {
            System.out.println("You have not entered a correct flavour");
            }
        } 
        return counter; 
    }

假设数据包是一个集合(并且您使用的是JDK 1.5或更高版本),您也可以使用以下方法:

public int countPacks(String flavor) {
   int numOccurrences = java.util.Collections.frequency(packets, flavor);
   if(numOccurrences == 0) {
      System.out.println("You have not entered a correct flavor");
   }
   return numOccurrences;
}

对象中声明的方法的一个问题是,在使用它们时缺乏静态类型检查。这同样适用于synchronized,在synchronized中,锁定错误的对象很常见

在本例中,Object.equals(Object)可用于任意两个对象。您使用了集合packets,而不是从中获取元素。重要的问题不是这个特定的bug是如何产生的,而是我们如何从一开始就防止它发生

首先,使用泛型并将迭代的每个元素分配给静态类型的局部元素:

public int countPacks(String flavour) {
    // [ The field represents a count. ]
    int count = 0;
    for(int index=0; index<packets.size(); ++index) {
        String packet = packets.get(index);
        if (packet.equals(flavour)) {
           ++count;
        }
    } 
    // [The error mesage was printed for each non-match.
    //  Even this is probably wrong,
    //    as it should be valid to have no packs of a valid flavour.
    //  Having the message in this method is actually a bit confused.]
    if (count == 0) {
        System.out.println("You have not entered a correct flavour");
    }
    return count; 
}
这看起来更清楚,但是一个好的程序员知道他/她的库

public int countPacks(String flavour) {
    int count = Collections.frequency(packets, flavour);
    if (count == 0) {
        System.out.println("You have not entered a correct flavour");
    }
    return count; 
}

您也可以考虑<代码> map <代码>,而不是集合。

public int countPacks(String flavour) {
    int count = Collections.frequency(packets, flavour);
    if (count == 0) {
        System.out.println("You have not entered a correct flavour");
    }
    return count; 
}