编译器不想执行一段代码
Compiler doesn't want to execute a chunk of code
我有一个方法可以打开一个文件并将每一行转换为一个对象(它可以是 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
。然后使用 for
循环将该数据分配给 tabChaineSafe
,然后使用另一个嵌套的 for
循环来处理该数据,但是,该循环应该 NOT 进入上一个循环,因为它毫无意义并且会多次处理数据。修复循环后,我们可以使用 System.out.println(...);
来确保值正常工作。
第二个问题是您的代码没有输入if
或else 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
语句并将输出打印到控制台:
Data loaded from scanner:
Livre,value1,value2,value
The if statement worked, and found Livre at tabChaineSafe[0]
现在我们知道上面的代码可以正常工作,您需要检查文件“doc.txt”是否确实包含您期望的数据。
我有一个方法可以打开一个文件并将每一行转换为一个对象(它可以是 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
。然后使用 for
循环将该数据分配给 tabChaineSafe
,然后使用另一个嵌套的 for
循环来处理该数据,但是,该循环应该 NOT 进入上一个循环,因为它毫无意义并且会多次处理数据。修复循环后,我们可以使用 System.out.println(...);
来确保值正常工作。
第二个问题是您的代码没有输入if
或else 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
语句并将输出打印到控制台:
Data loaded from scanner:
Livre,value1,value2,value
The if statement worked, and found Livre at tabChaineSafe[0]
现在我们知道上面的代码可以正常工作,您需要检查文件“doc.txt”是否确实包含您期望的数据。