Help with java program crashing when certain boolean types are entered
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Help with java program crashing when certain boolean types are entered

  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 09:04 AM. Reason: Added CODE tags

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    18,995

    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?
    Victor Nijegorodov

  3. #3
    Join Date
    Oct 2017
    Posts
    2

    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

  4. #4
    Join Date
    Feb 2017
    Posts
    165

    Re: Help with java program crashing when certain boolean types are entered

    Quote Originally Posted by Gruntlet01 View Post
    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.
    Last edited by wolle; October 29th, 2017 at 03:02 PM.

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)