【问题标题】:Compiler doesn't want to execute a chunk of code编译器不想执行一大段代码
【发布时间】:2021-04-13 01:52:07
【问题描述】:

我有一个方法可以打开一个文件并将每一行转换为一个对象(它可以是 DVD 或 Livre,都在超类 Document 下),然后放入一个数组中。但出于某种原因,在这些行之后:

try {
    tabChaineSafe[k] = tabChaine[k];
} catch (ArrayIndexOutOfBoundsException erreurarray) {
    tabChaineSafe[k] = "Information indisponible.";
}

编译器只是跳过其余部分。因此,整个数组最终充满了空值。这是我使用调试器查看会发生什么的 gif 图像。

https://gyazo.com/a09c2b617ba94f12dce1165420d528dd

这是该方法的完整代码:

public static Document[] remplirTabDocs(Document[] tabDocs) {
    String ligne = new String();
    String[] tabChaine;
    String[] tabChaineSafe = new String[6];
    FileInputStream file = lectureFichier("doc.txt");
    Scanner scan = new Scanner(file);
    int j = 0; //compteur pour le tableau d'objets
    while (scan.hasNext()) {
        ligne = scan.nextLine();
        tabChaine = ligne.split(",");
        for (int i = 0; i < tabChaine.length; i++) { //nettoyage des
            //espaces
            tabChaine[i] = tabChaine[i].trim();      //tableau des infos
            /*
             ** On va maintenant transférer ces String dans un nouveau
             ** String[] parce que parfois, il n'y a pas de description
             ** et cela peut causer des erreurs.
             */
            for (int k = 0; k < tabChaineSafe.length; k++) {
                try {
                    tabChaineSafe[k] = tabChaine[k];
                } catch (ArrayIndexOutOfBoundsException erreurarray) {
                    tabChaineSafe[k] = "Information indisponible.";
                }
                //
                if (tabChaineSafe[0].equalsIgnoreCase("Livre")) {
                    /*
                     ** Étape 1 : Vérifier si un ouvrage de ce nom est
                     ** déjà répertorié.
                     */
                    boolean existant;
                    existant = checkExistants
                            (tabChaineSafe[1], tabDocs);
                    if (existant) {
                        int numDocExistant = lequelExistant
                                (tabChaineSafe[1]
                                        , tabDocs);
                        tabDocs[numDocExistant].copiesDispo++;
                    } else {
                        Livre livre = new Livre(tabChaineSafe[0],
                                tabChaineSafe[1],
                                tabChaineSafe[2],
                                tabChaineSafe[3],
                                tabChaineSafe[4],
                                tabChaineSafe[5],
                                1);
                    }
                } else if (tabChaineSafe[0].equalsIgnoreCase("DVD")) {
                    /*
                     ** Étape 1 : Vérifier si un ouvrage de ce nom est
                     ** déjà répertorié.
                     */
                    boolean existant;
                    existant = checkExistants(tabChaineSafe[1],
                            tabDocs);
                    if (existant) {
                        int numDocExistant = lequelExistant
                                (tabChaineSafe[1]
                                        , tabDocs);
                        tabDocs[numDocExistant].copiesDispo++;
                    } else {
                        try {
                            int nbDisquesInt = Integer.parseInt
                                    (tabChaineSafe[3]);
                            DVD dvd = new DVD(tabChaineSafe[0],
                                    tabChaineSafe[1],
                                    tabChaineSafe[2],
                                    nbDisquesInt, 1);
                        } catch (NumberFormatException nbfmtexc) {
                            /*
                             ** Aucune instance n'est générée
                             */
                        }
                    }
                }
                //
            }
            j++;
        }
    }
    fermetureFichier(file);
    return tabDocs;
}

我不知道为什么会这样。同样对于上下文,将 tabChaine 的值转移到 tabChaineSafe 是必要的,因为有时文档中的一行缺少信息,我想避免这种情况导致属性被分配为 null。

【问题讨论】:

  • 如果tabChaine.length 小于tabChaineSafe 的长度会怎样?你会得到一堆空值。如果 tabChaine 由于扫描仪输入的输入问题而从未填写过会怎样?您将拥有一个空值数组。通过将值打印到控制台来进行一些调试,以确定从扫描仪读取的内容,以及 tabChaine 是否实际上被填充了值。此外,您的调试器会这样跳过的原因是您的代码不满足以下 if 或 else if 语句。
  • 这是我尝试 catch 的原因,当我检查调试器变量窗口时,tabChaine 和 tabChaineSafe 都可以正常工作
  • 这里显然存在不满足ifelse if 条件的问题,例如if ( tabChaineSafe[0].equalsIgnoreCase("Livre") ),tabChaineSafe[0] 是否永远等于“Livre”,显然不是如果我们使用您的调试器 gif。请编辑您的问题以包含更多信息,包括您放入扫描仪的一些示例值或我们可以测试的MCVE,或者编辑您的问题以在 try/catch 块之后显示调试器值。
  • 编译器不会执行你的代码。他们编译它。要精确。不清楚你在问什么。

标签: java arrays methods compilation


【解决方案1】:

您有几个问题需要解决,但让我们专注于主要问题。您的代码当前从扫描仪读取一行并将其拆分为数组tabChaine。然后使用for 循环将该数据分配给tabChaineSafe,然后使用另一个嵌套的for 循环来处理该数据,但是,该循环应该进入前一个循环,因为这是毫无意义的,并且会多次处理数据。修复循环后,我们可以使用System.out.println(...); 来确保这些值正常工作。

第二个问题是你的代码没有输入ifelse if语句:

if ( tabChaineSafe[0].equalsIgnoreCase("Livre") ) {
    //Code hidden for clarity
}
else if ( tabChaineSafe[0].equalsIgnoreCase("DVD") ) {
    //Code hidden for clarity
}

这是因为您没有在您期望的索引处从您的文件中获得您期望的值。我们可以通过使用一些示例数据ligne = "Livre,value1,value2,value";来检查它是否工作正常@

考虑以下工作示例,它将所有内容放在一起并使用示例数据打印正确的输出:

while (scan.hasNext())
{
    //Hide this line for now so that we can check our sample data:
    //ligne = scan.nextLine();

    //Sample data to use for testing:
    ligne = "Livre,value1,value2,value";

    //Print the data to console to see if it is loading tho correct thing
    System.out.println("Data loaded from scanner: \r\n"+ligne);

    //Split the data
    tabChaine = ligne.split(",");
    
    //Run the loop up to 6 times using "&& i < 6"
    //This will help prevent index out of bounds issues
    for (int i = 0; i < tabChaine.length || i < 6; i++)
    {
        //Trim the data and assign it directly to the safe loop if the index is not out of bounds
        if(i < tabChaine.length)
            tabChaineSafe[i] = tabChaine[i].trim();
        //If the index is out of bounds then assign a value
        else
            tabChaineSafe[i] = "no value found";
    }
    
    //Now that the loop is complete we can create the objects
    //This must be done outside of the loop above
    if (tabChaineSafe[0].equalsIgnoreCase("Livre"))
    {
        //Debug the values using the console:
        System.out.println("The if statement worked, and found " + tabChaineSafe[0] + " at tabChaineSafe[0]");

        //New you can do the rest of your code
        //boolean existant;
        //existant = checkExistants(tabChaineSafe[1], tabDocs);
        //if (existant)
        //{
        //    int numDocExistant = lequelExistant(tabChaineSafe[1],tabDocs);
        //    tabDocs[numDocExistant].copiesDispo++;
        //}
        //else
        //{
        //    livre = new Livre(tabChaineSafe[0],tabChaineSafe[1],tabChaineSafe[2],tabChaineSafe[3],tabChaineSafe[4],tabChaineSafe[5],1);
        //}
    }
    else if ( tabChaineSafe[0].equalsIgnoreCase("DVD") ) {
        System.out.println("DVD found");

        //Your code here
        //Removed for clarity
    }
    else
    {
        System.out.println("Unknown type fonud " + tabChaineSafe[0]);
    }
}
fermetureFichier(file);
return tabDocs;

使用我们的示例数据,它将正确输入if 语句并将输出打印到控制台:

从扫描仪加载的数据:

生活,价值1,价值2,价值

if 语句有效,并在 tabChaineSafe[0] 找到 Livre

既然我们知道上面的代码可以工作,你需要检查你的文件“doc.txt”是否真的包含你期望的数据。

【讨论】:

  • 非常感谢你,我真的没有意识到我所做的事情是多么不合逻辑。
猜你喜欢
  • 1970-01-01
  • 2014-11-28
  • 2015-10-25
  • 1970-01-01
  • 2012-04-28
  • 1970-01-01
  • 2021-12-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多