String substring & indexOf

Hi all, i am stuck with part of the code and cant figure out where is the problem.
Exercise is to play a game where you guess a movie title letter by letter until you lose or win (10 tries)

This is a part of the code where it is checked if guessed letter is same as any in the random movie title, if yes then we replace - - - - with letter on the index where it should be.

Code is working BUT if i press enter without entering letter then im getting this errors:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 2, end 7, length 6


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  // String underscoredTitle after guessed letter
     public String underscoredGuess(String randomMoviecopy, String guessLetter) {

        int pozicija=0;
        String tempUnderscoredTitle = "";

        for (int i=0;i<randomMoviecopy.length();i++) {
            pozicija = randomMoviecopy.indexOf(guessLetter,i);

                if(pozicija==i) {
                    tempUnderscoredTitle = underscoredTitle.substring(0, pozicija) + guessLetter + underscoredTitle.substring(pozicija+1,randomMoviecopy.length());
                    underscoredTitle = tempUnderscoredTitle;
                }

        }

        return underscoredTitle;
     }


underscoredTitle is variable inside of a class and it is letters of a movie title replaced with - - - - ...

sorry i just realised that im writing this question in c++ forum as i was learning c++ and this is the only programming forum im in so if its still working the same with java and c++ please please help
Last edited on
Code is working BUT if i press enter without entering letter then im getting this errors:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 2, end 7, length 6

This is C++. Java is a red headed stepchild of C++ so the syntax is very similar, but its not the same language.

that said, what happens if guess letter is not in the string? that may make pozicija invalid and cause it to crash; you may need something like if (pozicija != some_java_invalid_word) else (do nothing? guess is not found!)

you should join a java forum; some of us know it (Ive used it a tiny bit, but it can'd do 3/4 of what I want most of the time so I try not to) but its not really a great place to get this kind of help on a regular basis.
Last edited on
I am guessing that the exception is being thrown on the line with the substrings. You're doing a lot of string manipulation there, and I'd have to bet that doing underscoredTitle.substring(pozicija+1, randomMoviecopy.length()) is going out of bounds, because I assume randomMoviecopy.length() + pozicja+1 is well beyond the length of underscoredTitle.

I suggest debugging your program step by step and seeing the first spot where substring is going beyond the length of the full string, and work on fixing it from there.

You're doing the equivalent of "hangman" right? I would rather do something like this, where you keep the movie title string and the guesses as separate arrays that correspond to each other.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

// For example, if movie title is "Joker" and
// user guessed "k", filledGuesses would become "{f, f, t, f, f}"
// TODO: case sensitivity
public class MovieGuesser {

    MovieGuesser(String movieTitle) {
        this.movieTitle = movieTitle;
        this.filledGuesses = new boolean[movieTitle.length()];
        for (int i = 0; i < movieTitle.length(); i++) {
            if (movieTitle.charAt(i) == ' ')
                filledGuesses[i] = true;
            else
                filledGuesses[i] = false;
        }
    }

    public boolean guess(char guessLetter) {
        boolean correct_guess = false;
        
        for (int i = 0; i < movieTitle.length(); i++) {
            if (movieTitle.charAt(i) == guessLetter) {
                filledGuesses[i] = true;
                correct_guess = true;
            }
        }
        
        return correct_guess;
     }
     
     public String getProgress()
     {
         String progress = ""; // TODO: Could use StringBuilder here
         for (int i = 0; i < movieTitle.length(); i++)
         {
             if (filledGuesses[i])
             {
                 progress += movieTitle.charAt(i);
             }
             else
             {
                 progress += "_";
             }
         }
         
         return progress;
     }

     public static void main(String []args) {
        MovieGuesser guesser = new MovieGuesser("Wizard of Oz");
        
        boolean correct = guesser.guess('z');
        
        if (correct) {
            System.out.println("Correct!");
        } else {
            System.out.println("Incorrect!");
        }
        System.out.println(guesser.getProgress());
     }

    private String movieTitle;
    private boolean[] filledGuesses;
}

Correct!
__z___ __ _z

This completely avoids having to do error-prone substring manipulation every guess.

Also, thank you, you've let me remember why I hate java. I can't do simple things like "List<boolean>", I have to use the nullable Boolean unnecessary wrapper instead, and stupid things like "charAt".
Last edited on
I threw it in the garbage can after trying to translate a very simple crc32 ... into a langauge that for some unholy reason has no unsigned values... making me take 2-3 extra steps for every operation on top of its innate slowness. Yuck.
Topic archived. No new replies allowed.