SunQuest
           Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today!
  #1  
Old May 4th, 2008, 01:32 AM
angryface669 angryface669 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 1 angryface669 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 26 m 11 sec
Reputation Power: 0
Homework - Java 'Hangman' help

So, I am working on a hangman assignment, the program is supposed to run like this,

//Secret word _ _ _ _ _
//Wrong guesses:
//Guess a letter:e

//Secret word _ e _ _ _
//Wrong guesses:
//Guess a letter:r

//Secret word _ e _ _ _
//Wrong guesses: r
//Guess a letter:l

//Secret word _ e l l _
//Wrong guesses:
//Guess a letter:e
---------------------------------------------------------

So, the user enters a letter, and the program is supposed to check weather or not the letter is in the string (the secret word is chosen at random I highlighted the code) 'Secret word'. If the letter is in the string then it replaces the underscore, if it is incorrect the letter gets put in the 'Wrong guesses' list.

Here is what I have so far, this program just uses a for loop to allow the user to keep on guessing.

I cannot figure out how to replace the underscores or check weather or not the letter is in the string in the first place...
Any help would be nice...

Code:
java.util.*;
public class Testing{
        public static void main(String[] args){

                Scanner input = new Scanner(System.in);
                Scanner console = new Scanner(System.in);


                String answer = HangmanUtils.getWord();
                CountLetters(answer);
        }

        public static String GetWord (){
        String answer = HangmanUtils.getWord();
        return answer;
        }

        public static String CountLetters(String answer) {
                Scanner console = new Scanner(System.in);

                int i = 0;
                while((i >= 0) && (i <= 4)){
                i++;
                System.out.println();
                System.out.print("Secret word: "+answer);
                int count = 0;
                for (int j = 0; j< answer.length();j++) {
                        char ch = answer.charAt(j);
                        if (Character.isLetter(ch)){
                                count++;
                                System.out.print("_ ");
                        }
                }
                System.out.println();
                System.out.print("Wrong guesses: ");
                System.out.println();
                System.out.print("Guess a letter: ");
                char guess = console.next().charAt(0);
                        for (int k = 0; k <answer.length(); k++) {
                        if (answer.charAt(k) == guess) {
                                System.out.print(guess);
                        }else{
                        System.out.print(guess);
                }
        }
}
return answer;
}
}

Reply With Quote
  #2  
Old May 4th, 2008, 05:55 AM
sberg43 sberg43 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: Norway
Posts: 46 sberg43 User rank is Sergeant (500 - 2000 Reputation Level)sberg43 User rank is Sergeant (500 - 2000 Reputation Level)sberg43 User rank is Sergeant (500 - 2000 Reputation Level)sberg43 User rank is Sergeant (500 - 2000 Reputation Level)sberg43 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 11 h 2 m 29 sec
Reputation Power: 15
Quote:
Originally Posted by angryface669
I cannot figure out how to replace the underscores or check weather or not the letter is in the string in the first place...

I think you should start by looking at the methods in the String class. Try to use String.contains(..) and String.replace(...)

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Homework - Java 'Hangman' help


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway