• Post Reply Bookmark Topic Watch Topic
  • New Topic

Something is wrong with my string builder  RSS feed

 
Nikki Harding
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, I'm creating a program for school where I have to pull a random word from a text file (I'm using a predetermined smaller array for testing), scramble the word up, print it, and have the user guess what the original word was. The first pass through, everything works fine. But, when the user guesses correctly or asks for a new word, the scrambled word is shown as blank. For some reason after the loop the string builder stops working, and doesn't scramble the new word. Thanks in advance!



 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you copy the contents of the console from when you execute the program that shows what you are talking about and paste it here?
 
Nikki Harding
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what shows when I run it.

Current puzzle: boogdiwlgrn
Current points for word 10
Enter (g)uess, (n)ew word, (h)int, or (q)uit:
g
biglongword <- displays answer for testing purposes
Enter your guess:
biglongword
You guessed it!
picked word is textbook
Current puzzle: <- right here is the issue
Current points for word 10
Enter (g)uess, (n)ew word, (h)int, or (q)uit:
 
Emil Jennings
Ranch Hand
Posts: 75
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Might I suggest that there is nothing wrong with your string builder, it works just fine...when it executes (hint hint). Since you append letters to StringBuilder sb while (allTrue == false), what happens after you set allTrue to true? Or more importantly, what DOESN'T happen?
 
Carey Brown
Bartender
Posts: 3009
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see some possible problems in this section of code:

Where is allTrue initialized? Where is counter initialized? You seem to be counting letters that have already been picked, yet with random that could mean visiting the same letter over and over. Line 16 seems to be arbitrary. Why 2 * length?
 
Nikki Harding
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, that helped a bit! I changed the block to




Now it shows the scrambled word every time, but it is missing one character. Why is this happening?

example:
picked word is cats
Current puzzle: sca
Current points for word 10
Enter (g)uess, (n)ew word, (h)int, or (q)uit:

thank you so much by the way!
 
Nikki Harding
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:I see some possible problems in this section of code:

Where is allTrue initialized? Where is counter initialized? You seem to be counting letters that have already been picked, yet with random that could mean visiting the same letter over and over. Line 16 seems to be arbitrary. Why 2 * length?



I added that line because I couldn't quite figure out how to get it to stop scrambling, but if I only set it to once times the length sometimes it wouldn't scramble enough
 
Carey Brown
Bartender
Posts: 3009
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikki Harding wrote:
Carey Brown wrote:I see some possible problems in this section of code:

Where is allTrue initialized? Where is counter initialized? You seem to be counting letters that have already been picked, yet with random that could mean visiting the same letter over and over. Line 16 seems to be arbitrary. Why 2 * length?



I added that line because I couldn't quite figure out how to get it to stop scrambling, but if I only set it to once times the length sometimes it wouldn't scramble enough
That's why I was saying that you're counting the wrong thing. Lines 11-15 may be hit many times for a single character all depending on the "luck" of the random number generator. Lines 5-9 are only executed once for each character.
 
Nikki Harding
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Nikki Harding wrote:
Carey Brown wrote:I see some possible problems in this section of code:

Where is allTrue initialized? Where is counter initialized? You seem to be counting letters that have already been picked, yet with random that could mean visiting the same letter over and over. Line 16 seems to be arbitrary. Why 2 * length?



I added that line because I couldn't quite figure out how to get it to stop scrambling, but if I only set it to once times the length sometimes it wouldn't scramble enough
That's why I was saying that you're counting the wrong thing. Lines 11-15 may be hit many times for a single character all depending on the "luck" of the random number generator. Lines 5-9 are only executed once for each character.


I just tried moving the counter up to the != true block, but it gave me an infinite loop. Should I get rid of the else if used[random letter] == true section altogether? I apologize if my questions are silly, my code is really unorganized and I lost track of what it's doing
 
Carey Brown
Bartender
Posts: 3009
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikki Harding wrote:I just tried moving the counter up to the != true block, but it gave me an infinite loop. Should I get rid of the else if used[random letter] == true section altogether? I apologize if my questions are silly, my code is really unorganized and I lost track of what it's doing

Where are you initializing the counter? Are you still checking for counter > 2 * length? That will never happen.
 
Nikki Harding
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Nikki Harding wrote:I just tried moving the counter up to the != true block, but it gave me an infinite loop. Should I get rid of the else if used[random letter] == true section altogether? I apologize if my questions are silly, my code is really unorganized and I lost track of what it's doing

Where are you initializing the counter? Are you still checking for counter > 2 * length?


Here's what I have now:



I initialize the counter up at the top within the main method, set to 0. Outside of the while game = true loop
 
Carey Brown
Bartender
Posts: 3009
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikki Harding wrote:Here's what I have now:



I initialize the counter up at the top within the main method, set to 0. Outside of the while game = true loop

It is advisable to initialize variable just prior to using them. Hint, just before the while(). I thought you moved counter to the != block? And if you count only once for each letter, will counter ever be greater than length?
 
Nikki Harding
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Nikki Harding wrote:Here's what I have now:



I initialize the counter up at the top within the main method, set to 0. Outside of the while game = true loop

It is advisable to initialize variable just prior to using them. Hint, just before the while(). I thought you moved counter to the != block? And if you count only once for each letter, will counter ever be greater than length?


IT WORKS!! Thank you so so so much for your help!
 
Carey Brown
Bartender
Posts: 3009
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikki Harding wrote:IT WORKS!! Thank you so so so much for your help!

Congrats! You're welcome.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!