0

I am currently working on an issue with my code where after the first roll. currently, running the code looks like this...

Player 1 turn total is 0. Enter (r)oll or (s)top: r
Player 1 turn total is 0. Enter (r)oll or (s)top: You rolled: 3
Player 1 turn total is 3
 Enter (r)oll or (s)top: 
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:47)
    at java.base/java.lang.String.charAt(String.java:693)
    at Project3_Part1_Final.PlayerOneTurn(Project3_Part1_Final.java:58)
    at Project3_Part1_Final.main(Project3_Part1_Final.java:20)

I ran this through a free debugger online, but it did not help me. giving me the same errors, except for

at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:47)

Currently, whenever i roll a 1, the program does work, however it crashes whenever a number 2-6 is rolled. This is my code.

import java.util.Random;
import java.util.Scanner;

public class Project3_Part1_Final {
    static int playerScore = 0;
    static int playerTotal = 0;
    static int dice = 0;
    static final int FINAL_SCORE = 20;
    static int playerTwoScore = 0;
    static int playerTwoTotal = 0;
    static boolean gameOver = false;
    static boolean turnOver = false;
    static char repeat;
    static String input;
    static Scanner key = new Scanner(System.in);

    static Random rand = new Random();

    public static void main(String[] args) {
        PlayerOneTurn();
        PlayerTwoTurn();

    }

    public static void PlayerOneTurn(){
        
        System.out.print("Player 1 turn total is " + playerTotal);                  //welcome line
        System.out.print(". Enter (r)oll or (s)top: ");
        while(gameOver == false) {
            
            do {
                input = key.next();                                                 //input = next key entered
                repeat = input.charAt(0);
                System.out.print("Player 1 turn total is " + playerTotal);
                System.out.print(". Enter (r)oll or (s)top: ");
                
                if(repeat == 'r')                                                   //if input letter = r
                {
                    dice = rand.nextInt(6) + 1;                                     //set dice to number between 1-6
                    System.out.println("You rolled: " + dice);                      //display number, signifying the amount rolled
                    
                    if(dice == 1)                                                   //if the number rolled happens to be 1...
                    {
                        playerScore = 0;                                            //reset score to 0
                        System.out.print("You lose your turn! ");
                        System.out.println("Your total is " + playerTotal);
                        turnOver = true;                                            //go to next player.
                        while (playerTotal < 20);                                   //if the playerTotal is under 20
                            
                    }
                    
                    else 
                    {
                        playerScore += dice;                                        //add dice amount to player score
                        System.out.println("Player 1 turn total is " + playerScore);//print out total amount earned after last roll
                        System.out.println(" Enter (r)oll or (s)top: ");            //repeat question.
                        input = key.nextLine();
                        repeat = input.charAt(0);
                    }
                }
                else if (repeat != 'h')                                             //if neither option is called
                {
                    System.out.println("Incorrect entry, please try again");        //prompt retry
                    input = key.next();
                    
                }
                    
                else 
                {
                    break;
                }

                
            }while(turnOver == false || dice != 1);
            break;
        }
    }

    public static void PlayerTwoTurn() {
        System.out.println("success"); //test line


    }
}

I am sure there is a very simple answer to this question, but I have been struggling to find it. I dont need exact code, but if anyone has some ideas on what i can do to fix this problem, i would greatly appreciate it.

P.S. i had the PlayerOneTurn() method working a while back, but i started from scratch, and something in that code messed up while transferring working sections. I am sure i am not that far off from it.

Dennis Jaheruddin
  • 19,745
  • 7
  • 58
  • 100
  • as a matter of fact it did! thank you! now i just need to fix some strings to update the score. thank you though! I didnt even notice that line while looking through. – Andrew Argie Jul 14 '20 at 19:11

0 Answers0