![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: May 2006
Posts: 39
Rep Power: 0
![]() |
Creating two lists of correct and incorrect letters.
I'm making a 'guess the word' program.
The idea is a user presses a key on the keyboard and if the letter was in the word it gets stored in a list of all the letters that have been guessed right, otherwise its stored in alist of all the incorrect guesses. I'm trying to do it with arrays, I'm initializing two array's with 26 indices (bit of overkill but better safe than sorry), and the idea is if the user inputs a correct letter its input in one array and vise versa. However lets say if the first guess was correct, I store the letter in position x of the array, and print it, the array prints 25 blanks after it. Can this be averted? |
|
|
|
|
|
#2 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 773
Rep Power: 3
![]() |
By "guess the word" I'm assuming you mean something like hangman. I'm not sure why you get 25 spaces. Could you post your code?
|
|
|
|
|
|
#3 |
|
Programmer
Join Date: May 2006
Posts: 39
Rep Power: 0
![]() |
umm, I sort of deleted it, the code was very messy...
It was something like this: char[] correct = new char[26]
char[] incorrect = new char[26]
char <letter>
wordlength = word.length;
int x = 0;
while ( 0 < wordlength )
{
if ( charAt(x) == letter )
{
correct[x] = charAt(x);
x = 0;
while ( x < 26;)
{
System.out.print(correct[x]);
}
break;
}
}I think it was something like that, it was very muddled so I decided to start over from scratch... |
|
|
|
|
|
#4 |
|
Newbie
Join Date: May 2006
Posts: 28
Rep Power: 0
![]() |
never delete your code, it might come in handy later.
Anyways, when you print out your array, you can check for a blank space, or null. I cant remember what an empty value of an array is, i think its null. Check against null and only print out if its !null. |
|
|
|
|
|
#5 |
|
Programming Guru
![]() |
@tumbleTetris: To answer your question, you could keep track of how many letters have been added to each array and only print the value of the indices up to that number. Or you could use NScharr's idea and only print the values of the array that are not null (or whatever the default value of a char[] array is).
I believe a better way to keep track of the letters that have been entered would be to use an ArrayList. (If you are worried about speed, which I doubt, you can use the initialCapacity constructor.) This way, you can simply add letters to either list and print the list, which will contain only those letters. Last edited by titaniumdecoy; May 22nd, 2006 at 9:16 PM. |
|
|
|
|
|
#6 |
|
Programmer
Join Date: May 2006
Posts: 39
Rep Power: 0
![]() |
thanks for the help guys.
I have a technical question, if lets say... int x = y; basically whatever int value y is x becomes right? Lets say the next line I increment y by 1, y++; does that mean x is also incremented by 1? |
|
|
|
|
|
#7 |
|
Newbie
Join Date: May 2006
Posts: 28
Rep Power: 0
![]() |
To add, i also agree with titaniumdecoy that you should use an ArrayList for a few reasons. In this application the speed and memory usage wont matter, however, using the ArrayList approach will make you learn how to use it, which will definitally help with future projects where space is an issue. it also eliminates any need to check through the whole array, as you only go through the list and then you are done.
|
|
|
|
|
|
#8 |
|
Programming Guru
![]() |
@tumbleTetris: Why don't you try it?
If you're interested in understanding how this sort of thing works in Java, I recently found this article especially enlightening. |
|
|
|
|
|
#9 |
|
Expert Programmer
Join Date: Dec 2004
Posts: 794
Rep Power: 5
![]() |
tumbleTetris: it does in some languages, doesn't in others. I don't know about Java. Why am I even in this forum?
__________________
Few people deserve to be compared to (Rush) Limbaugh, most of them were convicted at the Nuremburg trials. --WilliamSChips on Slashdot |
|
|
|
|
|
#10 | |
|
Programmer
Join Date: May 2006
Posts: 39
Rep Power: 0
![]() |
Quote:
Yeah, I'll try it out. I'm relatively sure that it just stores the value at that moment into the int variable though... |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|