Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-11-2007, 09:02 PM
Member
 
Join Date: Dec 2007
Location: Philly
Posts: 3
sondratheloser is on a distinguished road
Send a message via AIM to sondratheloser
Array traversal issues
I am trying to make a simple hangman program, and I'm working on getting the framework set up. The problem is, I'm pretty sure, in the if statements in the compare method the int whichletter is supposed to be incremented in each run through the for loop in the main method (8 times, it should be) so that it reads the next letter of the randomly chosen 8-letter word, but I think it's just staying at charAt(0) for some reason.. here is the code..

note: I know there are other problems with it probably, but I'm just kind of stuck on this one right now.

Code:
import java.io.*; import java.util.*; public class stuff {//begin classbody public static void main (String[]args)throws IOException {//BEGIN MAIN String wordchosen = PickRandomWord(); System.out.println(wordchosen); int whichletter =0; for (int cnt1=0; cnt1<8; cnt1++) { String guessedletter=guess(); System.out.println("you guessed " + guessedletter); compare(wordchosen, guessedletter, whichletter); whichletter++; } }//END MAIN public static String PickRandomWord() {//begin pickrandom //words to be picked from String wordchosen; String [] word = new String [5]; word [0] ="aardvark"; word [1] ="trioxide"; word [2] ="chromium"; word [3] ="lungworm"; word [4] ="zombiism"; //generate a random number Random randomword = new Random(); int random = randomword.nextInt( 5 ); wordchosen = word[random]; return wordchosen; }//end pickrandom public static String guess() throws IOException {//BEGIN GUESS BufferedReader in = new BufferedReader (new InputStreamReader(System.in)); System.out.println("Please Enter your guess. (all lowercase, please)"); String guessedletter = in.readLine(); return guessedletter; }//END GUESS public static void compare (String wordchosen, String guessedletter, int whichletter) {//BEGIN COMPARISON int right = 0; int wrong = 0; int length = 8; if (guessedletter.charAt(whichletter) == wordchosen.charAt(whichletter)) { right++; } if (guessedletter.charAt (whichletter) != wordchosen.charAt(whichletter)) { wrong++; } System.out.println("You guessed " + wrong + " letters incorrectly;."); System.out.println("You guessed " + right + " letters correctly;."); }//END COMPARISON }//end class body
Help appreciated!
-Sondra
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-11-2007, 09:55 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 123
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
Instead of using your whichletter int, you could just use the cntl int that you are using to control your for loop. This should insure that your number is incrementing.

Also, in your if statements
Code:
if (guessedletter.charAt(whichletter) == wordchosen.charAt(whichletter)) { right++; }
I think it should be

Code:
if (guessedletter.charAt(0) == wordchosen.charAt(whichletter)) { right++; }
Unless I am mistanken, the guessedLetter is overwritten each time and only contains a single character correct?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bidirectional Traversal with ListIterator Java Tip java.lang 0 04-17-2008 12:37 AM
Issues with Jva I.O Annatar01 New To Java 0 02-08-2008 03:16 AM
Array issues Neo82 New To Java 1 12-31-2007 05:22 AM
JTable Focus Traversal helios_lie AWT / Swing 1 12-20-2007 12:27 PM
Preorder Traversal problem... tuckker New To Java 3 12-04-2007 08:06 AM


All times are GMT +3. The time now is 04:41 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org