Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 22nd, 2006, 3:04 AM   #1
tumbleTetris
Programmer
 
Join Date: May 2006
Posts: 39
Rep Power: 0 tumbleTetris is on a distinguished road
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?
tumbleTetris is offline   Reply With Quote
Old May 22nd, 2006, 3:57 AM   #2
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 773
Rep Power: 3 Jimbo is on a distinguished road
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?
Jimbo is offline   Reply With Quote
Old May 22nd, 2006, 4:05 AM   #3
tumbleTetris
Programmer
 
Join Date: May 2006
Posts: 39
Rep Power: 0 tumbleTetris is on a distinguished road
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...
tumbleTetris is offline   Reply With Quote
Old May 22nd, 2006, 2:47 PM   #4
NSchnarr
Newbie
 
Join Date: May 2006
Posts: 28
Rep Power: 0 NSchnarr is on a distinguished road
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.
NSchnarr is offline   Reply With Quote
Old May 22nd, 2006, 8:55 PM   #5
titaniumdecoy
Programming Guru
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 1,007
Rep Power: 5 titaniumdecoy will become famous soon enough
Send a message via AIM to titaniumdecoy
@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.
titaniumdecoy is offline   Reply With Quote
Old May 22nd, 2006, 10:56 PM   #6
tumbleTetris
Programmer
 
Join Date: May 2006
Posts: 39
Rep Power: 0 tumbleTetris is on a distinguished road
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?
tumbleTetris is offline   Reply With Quote
Old May 22nd, 2006, 10:59 PM   #7
NSchnarr
Newbie
 
Join Date: May 2006
Posts: 28
Rep Power: 0 NSchnarr is on a distinguished road
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.
NSchnarr is offline   Reply With Quote
Old May 23rd, 2006, 1:23 AM   #8
titaniumdecoy
Programming Guru
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 1,007
Rep Power: 5 titaniumdecoy will become famous soon enough
Send a message via AIM to titaniumdecoy
@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.
titaniumdecoy is offline   Reply With Quote
Old May 23rd, 2006, 1:31 AM   #9
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 5 uman is on a distinguished road
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
uman is offline   Reply With Quote
Old May 23rd, 2006, 2:38 AM   #10
tumbleTetris
Programmer
 
Join Date: May 2006
Posts: 39
Rep Power: 0 tumbleTetris is on a distinguished road
Quote:
Originally Posted by uman
tumbleTetris: it does in some languages, doesn't in others. I don't know about Java. Why am I even in this forum?
good question...

Yeah, I'll try it out.

I'm relatively sure that it just stores the value at that moment into the int variable though...
tumbleTetris is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:27 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC