首页 > 解决方案 > 将字符串值与布尔值进行比较

问题描述

我正在尝试将布尔值(所有这些都是0)与这个字符串数组的值进行比较data[] = "1,0,0,0,0,0,0,0,0,0,0,0,1"。这是我的代码:

recipeIngrediants = new boolean[numberOfCuisines][numberOfIngrediants];
        int i = 0;
        while(file.hasNextLine()){
            String temp = file.nextLine();
            String[] data = temp.split(",");
        for(int j=0; j < recipeIngrediants.length; j++){
            String c = data[j];
            if(c == "1"){
                recipeIngrediants[i][j] = true;
            }
            else{
                recipeIngrediants[i][j] = false;
            }
        }
        i++;
     }

我收到一个错误,说它是类型不匹配。编辑:修复了类型不匹配错误,但对于布尔值中的所有值,它仍然给我一个 false 值

问题:

我还能如何比较这些值以使 2D 数组在具有 a的任何位置都recipeIngrediants相等?truedata1

标签: javamultidimensional-array

解决方案


我认为您想为每个评估为true的字符串分配数组项,否则:c"1"false

recipeIngrediants = new boolean[numberOfCuisines][numberOfIngrediants];
int i = 0;
while(file.hasNextLine()){
    String temp = file.nextLine();
    String[] data = temp.split(",");
    for(int j=0; j < recipeIngrediants.length; j++){
        String c = data[j];
        recipeIngrediants[i][j] = (c != null) && (c.equals("1"));
    }
    i++;
}

推荐阅读