CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Threaded View

  1. #1
    Join Date
    Oct 2017
    Posts
    2

    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.
    Last edited by VictorN; October 29th, 2017 at 08:04 AM. Reason: Added CODE tags

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured