Help with java program crashing when certain boolean types are entered
Hi all, I am a new A level computer science student and am currently struggling with a piece of code that I have written. That code is as follows:
Code:
import java.util.Scanner;
public class Question5{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
Boolean cont = true;
String actanswer = "RANDOM";
System.out.println("I am thinking of a 6 letter word. Start guessing!");
while(cont == true){
System.out.println("Please enter your guess: ");
String crntguesslower = sc.nextLine();
String crntguess = crntguesslower.toUpperCase();
String[] parts = crntguess.split("");
String part1 = parts[0];
String part2 = parts[1];
String part3 = parts[2];
String part4 = parts[3];
String part5 = parts[4];
String part6 = parts[5];
char letter1 = 'a';
char letter2 = 'b';
char letter3 = 'c';
char letter4 = 'd';
char letter5 = 'e';
char letter6 = 'f';
switch (part1){
case "R": letter1 = 'R';
break;
default: letter1 = '?';
}
switch (part2){
case "A": letter2 = 'A';
break;
default: letter2 = '?';
}
switch (part3){
case "N": letter3 = 'N';
break;
default: letter3 = '?';
}
switch (part4){
case "D": letter4 = 'D';
break;
default: letter4 = '?';
}
switch (part5){
case "O": letter5 = 'O';
break;
default: letter5 = '?';
}
switch (part6){
case "M": letter6 = 'M';
break;
default: letter6 = '?';
}
System.out.println("Result: " + letter1 + letter2 + letter3 + letter4 + letter5 + letter6);
String answercheck = crntguess;
switch(answercheck){
case "RANDOM": System.out.println("Congratulations, you have guessed correctly! Would you like to continue? True/False");
break;
default: System.out.println("Sorry, you have not yet guessed correctly, would you like to try again? True/False");
}
cont = sc.nextBoolean();
}
}
}
My issue is that every time the user types 'True' to continue and guess again, the program crashes with the error:
java.lang.ArrayIndexOutOfBoundsException: 1
at Question5.main(Question5.java:14)
Any help at all with fixing this would be greatly appreciated.
Re: Help with java program crashing when certain boolean types are entered
Did you try to debug your code (step-by-step!) to see what and where goes wrong or not expected?
Re: Help with java program crashing when certain boolean types are entered
As I detailed previously, I have only just started learning to code Java at college and therefore have not yet learnt how to debug. My code works fine when the user types false to end the program, but if they type true to be given another attempt the program crashes.
Many thanks :)
Re: Help with java program crashing when certain boolean types are entered
Quote:
Originally Posted by
Gruntlet01
not yet learnt how to debug.
Debugging means you try to figure out what's happening in your program by tracing the flow and look whether variables are holding expected values.
The error message indicates an array is accessed out of bounds at line 14 and that must be String[] parts.
Therefore as a first debugging step I suggest you print crntguess to see what it really holds. Again the error message indicates the array has size 1 and thus holds just one element. This would mean String part2 = parts[1]; is causing the error because parts[1] would then be an access outside the array boundary.
It could be crntguess.split(""); that isn't doing the expected. By printing the parts array you can find out.