CaptainAargh CaptainAargh - 4 months ago 6
Java Question

Try/catch doesn't run full code

In the following code I'm trying to split each line of an textfile called "Opgave1.txt" in two parts. The first word goes in the array "oplossingWoord" the rest of that line goes in the array "beschrijving". That part of the code works.

But the second part where I want to convert the array "oplossingWoord" into an char array list "oplossingLettersLijst" doesn't run. I have no idea why. At the end I also try to sout them but the output just stays blank. Hopefully you guys can help me.

Thanks in advance!

The code:

public class LetterPyramide {
public static String[] oplossingWoord = new String[8];
public static String[] beschrijving = new String[8];
public static List<Character> oplossingLettersLijst = new ArrayList<Character>();
public static String OpgaveKeuze;


public LetterPyramide() {
try {
File OPGAVEN_FILE = new File("Opgave1.txt");
int oplossingLettersIndex = 0;
Scanner s1 = new Scanner(OPGAVEN_FILE);
for (int i = 0; i < 8; i++) {
String[] line = s1.nextLine().split("\\s+");
oplossingWoord[i] = line[0];
if (line.length > 2) {
for (int j = 1; j < line.length; j++) {
String oudeWaarde = line[1];
beschrijving[i] = oudeWaarde + " " + line[j];
}
} else {
beschrijving[i] = line[1];
}

}
for (int i = 0; i < oplossingWoord.length; i++) {
for (int j = 0; j < oplossingWoord[i].length(); j++) {
oplossingLettersLijst.add(oplossingLettersIndex, oplossingWoord[i].charAt(j));
}
}

System.out.println("------------Beschrijving------------");
for (int i = 0; i < beschrijving.length; i++) {
System.out.println(i + "." + beschrijving[i]);
}
System.out.println("----------Oplossingen---------------");
for (int i = 0; i < oplossingWoord.length; i++) {
System.out.println("woord " + i + ": " + oplossingWoord[i]);
for (int j = 0; j < oplossingLettersLijst.size(); j++) {
System.out.print(oplossingLettersLijst.get(j));
}
}

} catch (Exception e) {

}
}


The textfile:

LA Opbergplaats
BAL Speelgoed
BALE Voetballer
BLASE Dikkenekkerig
LOEBAS Boerenkinkel
SLABOER Groentenkweker
BROLSATE Slecht vlees op een stokj


EDIT: Fixed it all thanks to the stackTrace. Thank you guys!

Answer

Most probably your code runs into an exception. Add a e.printStackTrace() to your catch clause and execute your application again to see what is printed on the console.

try {
    // your code
} catch (Exception e) {
    e.printStackTrace();
}

That should help.