• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Hangman game bug. User input - String out of bounds exception

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

Trying to build Hangman.

My "selectWord" , "readIntoArr", and "wordChoiceLenUnderscore" functions all work properly. I'm able to get IO and to choose a random word from my word file. However, I'm running into a bug and I've narrowed it down to the code text in bold below for where I think I might be making a mistake. In my run function. I have a for loop that loops through the number of characters in the chosen word.
I assign the user input to the "input" variable as well as the correct letter to the "answerChar" variable. I then compare the two, if the user types in the correct letter, it prints out the input, else, I execute a draw function in another class that draws the head. I didn't include the code for the other class as it's mostly just the visual drawing of the hangman.

When I run the program. I'm able to type in a letter. If I type in the wrong letter, it prompts the user to try again. I then type another letter, and I get a StringOutOfBoundsException: String index out of range: 1. What I did notice is that, if I type in 2 letters, then following that, 3 letters, it will accept the input. So i'm assuming I'm calling the wrong function for char input = scan.next().charAt(i)?  Line 33 - 34

 
Saloon Keeper
Posts: 8704
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may have  meant this
 
brian leung
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I made some edits on my run(), however still running into logic errors when I run my program.

When I try to debug my program,so let's say I get the word "Road" selected at random. If I type in R, O, A, D. Everything appears to work fine, the game ends and prints "You Win"!

Couple of problems

Problem #1
Say I'm playing the game properly, chances are pretty low that I get the first letter correct. Assuming I still have the word "Road". If i type in "E". It prints out the incorrect letter and executes the first draw function. However, if I try to then type in "R" which is the correct letter. It still says it's an incorrect input.

Problem #2
Similar to the first problem. Say I type in "R" as my first input. It's correct, loop iterates. I then type in "E" as my second input. It prints out the incorrect letter, I try again this time with the correct letter "O", it still doesn't accept it as correct input.

I'm assuming this might have something to do with

char input = scan.next().charAt(0);

I tried to put (i) so that it iterates with the loop, but then I get an Index out of bounds exception.

 
Rancher
Posts: 4504
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 I get an Index out of bounds exception.


Please post the statement where that happens and the value of the index.

One problem I see on line 21 is the i <= 6 expression.  Why the hard coded value 6?  It would be better to use a variable or the .length.
That expression will cycle through the 7 index values: 0 to 6.  
 
brian leung
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:

 I get an Index out of bounds exception.


Please post the statement where that happens and the value of the index.

One problem I see on line 21 is the i <= 6 expression.  Why the hard coded value 6?  It would be better to use a variable or the .length.
That expression will cycle through the 7 index values: 0 to 6.  



Please post the statement where that happens and the value of the index.

It always says "String index out of range: 1". Basically it's always the 2nd input that I put in, regardless of whether the input is correct or incorrect I still get it.

One problem I see on line 21 is the i <= 6 expression.  Why the hard coded value 6?  It would be better to use a variable or the .length.
That expression will cycle through the 7 index values: 0 to 6.


You're right, I should probably just leave it at i < 6. The reason why I chose 6, is because it is Hangman. The player only has 6 tries before the game is over. So the whole run() iterates 6 times.
 
Carey Brown
Saloon Keeper
Posts: 8704
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Player may have 6 tries but the loop is dependent on the length of the chosen word regardless.
 
Norm Radder
Rancher
Posts: 4504
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It always says "String index out of range: 1".


What line does that happen on?
 
brian leung
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Player may have 6 tries but the loop is dependent on the length of the chosen word regardless.



Hmm you're right. Thanks Carey, looks like I might have to rewrite/ organize the run().
 
brian leung
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:

It always says "String index out of range: 1".


What line does that happen on?



It happens on Line 23.
 
Norm Radder
Rancher
Posts: 4504
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How long is the contents of wordChoice?  The exception says it has less than 2 characters: no char at index 1.
The above was looking at line 24.

It happens on Line 23.


The index on line 23 is 0.  But the error message you posted said the index was 1???
 
Carey Brown
Saloon Keeper
Posts: 8704
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:How long is the contents of wordChoice?  The exception says it has less than 2 characters: no char at index 1.


No, this is the input for the single character that is being guessed.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic