943,875 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1155
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 23rd, 2008
0

need help regarding hangman like thing

Expand Post »
hi guys how r u doing .. this is my first post ..
i have this code .. the problem it runs .. the following happens:
when it says: geef een woord (= give a word ) i write the word ..
then it says geef een letter ( = try to guess using a letter) ..
if the word is for example: soccer ..
and i write: s
then i get: s.....
then i add the letter: o
i get: .o.... so instead of getting: so.... it does not show
the previous result ..

this is my code .. i hope u could help:
Java Syntax (Toggle Plain Text)
  1. import java.util.*;
  2. public class Scanner3
  3. {
  4. public static void main(String args[])
  5. {
  6. //Object aanmaken
  7. Scanner woordin= new Scanner(System.in);
  8. //Aanmaken variabele
  9.  
  10. String gwoord;
  11. //printen van het tekst
  12. System.out.print("Geef een woord: ");
  13. //Woord in de variabele zetten
  14. gwoord=woordin.nextLine();
  15. //String buffer puntjes wordt aangemaakt
  16. StringBuffer puntjes = new StringBuffer();
  17. //Object letterin wordt aangemaakt van de klasse scanner
  18. Scanner letterin = new Scanner(System.in);
  19. //Variabele letter wordt aangemaakt
  20. String letter;
  21. //Printen van het tekst
  22. boolean Geraden = false;
  23. while(Geraden == false)
  24. {
  25. System.out.print("Geef een letter: ");
  26. //de letter wordet ingevoerd
  27. letter=letterin.nextLine();
  28. //String puntje wordt aangemaakt en er wordt een puntje als karakter ingezet
  29. String puntje = ".";
  30. //herhalen tot de aantal letters vanhet in gevoerde woord
  31. for (int i = 0; i < gwoord.length( ); i++)
  32. {
  33. // Als het lettertje voorkomt op index i
  34. if( gwoord.charAt( i ) == letter.charAt( 0 ) )
  35. {
  36. // Juiste letter invoegen
  37. puntjes.append(letter.charAt( 0 ));
  38. }
  39. else
  40. {
  41. // Anders het puntje plaatsen
  42. puntjes.append(puntje.charAt( 0 ));
  43. }
  44. }
  45. //Het puntjes woord wordt geprint op het scherm
  46. System.out.println("Geraden letters: "+ puntjes );
  47. puntjes.delete(0,gwoord.length());
  48. //if(gwoord.equals(totnugeraden))
  49. //{
  50. //Geraden = true;
  51. //}
  52. }
  53. }
  54. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Modo is offline Offline
35 posts
since Oct 2008
Oct 23rd, 2008
0

Re: need help regarding hangman like thing

This is happening because of you are initializing puntje inside the while loop. So every time it loops, it creates new object called puntje; which necessarily doesn't remember the old values. Your comments are not clear to me, as I can't read them. Why are you putting "." in puntje?
Try the following; you will get something better:
Java Syntax (Toggle Plain Text)
  1. String puntje = ".";
  2. while(Geraden == false)
  3. {
  4. System.out.print("Geef een letter: ");
  5. //de letter wordet ingevoerd
  6. letter=letterin.nextLine();
  7. //String puntje wordt aangemaakt en er wordt een puntje als karakter ingezet
  8. //herhalen tot de aantal letters vanhet in gevoerde woord
  9. for (int i = 0; i < gwoord.length( ); i++)
  10. {
  11. // Als het lettertje voorkomt op index i
  12. if( gwoord.charAt( i ) == letter.charAt( 0 ) )
  13. {
  14. // Juiste letter invoegen
  15. puntjes.append(letter.charAt( 0 ));
  16. }
  17. else
  18. {
  19. // Anders het puntje plaatsen
  20. puntjes.append(puntje.charAt( 0 ));
  21. }
  22. }
  23. //Het puntjes woord wordt geprint op het scherm
  24. System.out.println("Geraden letters: "+ puntjes );
Reputation Points: 46
Solved Threads: 11
Junior Poster
orko is offline Offline
164 posts
since Apr 2006
Oct 23rd, 2008
0

Re: need help regarding hangman like thing

Sorry .. i should have translated the comments .. but as i saw u only deleted:
puntjes.delete(0,gwoord.length());
this causes the letters to be multiplied ..
so if write s .. from soccer i get s.....
then i add r i get: s....r....
thats weird too ..
as for string puntje = "."
i made the point be a char .. this way to display the points of the letters that are not guessed yet ..
Reputation Points: 10
Solved Threads: 0
Light Poster
Modo is offline Offline
35 posts
since Oct 2008
Oct 23rd, 2008
0

Re: need help regarding hangman like thing

Hey! You got me wrong. I didn't delete anything. I just moved the following line:
Java Syntax (Toggle Plain Text)
  1. String puntje = ".";
You also have to use replace function instead of append. It should be like this:
Java Syntax (Toggle Plain Text)
  1. String puntje = "";
  2. for(int i=0; i<gwoord.length(); i++) {
  3. puntje.append(".");
  4. }
  5. while(Geraden == false)
  6. {
  7. System.out.print("Geef een letter: ");
  8.  
  9. letter=letterin.nextLine();
  10.  
  11. for (int i = 0; i < gwoord.length( ); i++)
  12. {
  13.  
  14. if( gwoord.charAt( i ) == letter.charAt( 0 ) ) //I assume here character matches
  15. {
  16. //Here you were doing wrong:
  17. puntjes.replace(puntje.charAt(i), letter.charAt( 0 ));
  18. }
  19. else //I assume the character didn't match
  20. {
  21. //do nothing
  22. }
  23. }
  24.  
  25. System.out.println("Geraden letters: "+ puntjes );
  26. puntjes.delete(0,gwoord.length());
  27. }
  28. }
Reputation Points: 46
Solved Threads: 11
Junior Poster
orko is offline Offline
164 posts
since Apr 2006
Oct 23rd, 2008
0

Re: need help regarding hangman like thing

thanks a lot Orko .. it compiles .. but the main problem is that it says:

String index out of range

this is my code now

Java Syntax (Toggle Plain Text)
  1. import java.util.*;
  2. public class Scanner32
  3. {
  4. public static void main(String args[])
  5. {
  6. //Object aanmaken
  7. Scanner woordin= new Scanner(System.in);
  8. //Aanmaken van variabele
  9.  
  10. String gwoord;
  11. //printen van de tekst
  12. System.out.print("Geef een woord: ");
  13. //Woord in de variabele zetten
  14. gwoord=woordin.nextLine();
  15. //Stringbuffer puntjes wordt aangemaakt
  16. StringBuffer puntjes = new StringBuffer();
  17. //Object letterin wordt aangemaakt van de klasse scanner
  18. Scanner letterin = new Scanner(System.in);
  19. //Variabele letter wordt aangemaakt
  20. String letter;
  21. //String puntje wordt aangemaakt en er wordt een puntje als karakter ingezet
  22. boolean Geraden = false;
  23. String puntje = ".";
  24. //for(int i=0; i<gwoord.length(); i++)
  25. {
  26. puntjes.append(".");
  27. }
  28. while(Geraden == false)
  29. {
  30. //Printen van het tekst
  31. System.out.print("Geef een letter: ");
  32. //de letter wordet ingevoerd
  33. letter=letterin.nextLine();
  34.  
  35. //herhalen tot de aantal letters vanhet in gevoerde woord
  36. for (int i = 0; i < gwoord.length( ); i++)
  37. {
  38. // Als het lettertje voorkomt op index i
  39. if( gwoord.charAt( i ) == letter.charAt( 0 ) )
  40. {
  41. // Juiste letter invoegen
  42. puntje.replace(puntje.charAt(i), letter.charAt(0));
  43. }
  44. else
  45. {
  46. // Anders het puntje plaatsen
  47. puntjes.append(puntje.charAt( 0 ));
  48. }
  49. }
  50. //Het puntjes woord wordt geprint op het scherm
  51. System.out.println("Geraden letters: "+ puntjes );
  52. puntjes.delete(0,gwoord.length());
  53. //if(gwoord.equals(totnugeraden))
  54. //{
  55. //Geraden = true;
  56. //}
  57. }
  58. }
  59. }
Last edited by Modo; Oct 23rd, 2008 at 4:21 pm.
Reputation Points: 10
Solved Threads: 0
Light Poster
Modo is offline Offline
35 posts
since Oct 2008
Oct 23rd, 2008
0

Re: need help regarding hangman like thing

You got two variables "puntje[type: String]" and "puntjes[typetringBuffer]".
Here in your last code I don't see any where you used "puntje". There are few other silly stuffs. Like you are already replacing, so there is no need to delete each time. Try this:
        StringBuffer puntjes = new StringBuffer(); 
        Scanner letterin = new Scanner(System.in);
        String letter; 
        boolean Geraden = false;
        //You don't need this here: String puntje = ".";

        for(int i=0; i<gwoord.length(); i++) 
        {
               puntjes.append(".");
        }
	String puntje = puntjes.toString();
        while(Geraden == false)
        {
          System.out.print("Geef een letter: ");
          letter=letterin.nextLine();

          for (int i = 0; i < gwoord.length( ); i++) 
          {

            if( gwoord.charAt( i ) == letter.charAt( 0 ) )
            {
                puntje.replace(puntje.charAt(i), letter.charAt(0));
            }
            else
            {
		//Do nothing!!!!
            }
          }
          System.out.println("Geraden letters: "+ puntje );
          // You Don't Have to delete anything!!! You are already replacing!!!!puntjes.delete(0,gwoord.length());
    
       }
Last edited by orko; Oct 23rd, 2008 at 4:46 pm.
Reputation Points: 46
Solved Threads: 11
Junior Poster
orko is offline Offline
164 posts
since Apr 2006
Oct 23rd, 2008
0

Re: need help regarding hangman like thing

thanks orko ..
the out of index is solved
but now when i type a letter java only displays dots even if i guessed the letter right ..
how come?
i did exactly how u told me .. my code without comments look like this:

import java.util.*;
public class Scanner32
{
    public static void main(String args[]) 
    {        
         Scanner woordin= new Scanner(System.in);
         String gwoord;      
         System.out.print("Geef een woord: ");
         gwoord=woordin.nextLine();
         StringBuffer puntjes = new StringBuffer(); 
         Scanner letterin = new Scanner(System.in);
         String letter; 
         boolean Geraden = false;
         for(int i=0; i<gwoord.length(); i++) 
        {
               puntjes.append(".");
        }
        String puntje = puntjes.toString();
        while(Geraden == false)
        {
               System.out.print("Geef een letter: ");
               letter=letterin.nextLine();
               for (int i = 0; i < gwoord.length( ); i++) 
        {
                 if( gwoord.charAt( i ) == letter.charAt( 0 ) )
            {
                     puntje.replace(puntje.charAt(i), letter.charAt(0));
            }
            else
            {
             }
        }
        System.out.println("Geraden letters: "+ puntjes );
      }
    }
}
Reputation Points: 10
Solved Threads: 0
Light Poster
Modo is offline Offline
35 posts
since Oct 2008
Oct 24th, 2008
0

Re: need help regarding hangman like thing

So the characters are not getting replaced. Read the API specification for String and StringBuffer. You will get it. I did not compile the code I have given you; idea was to show you how to do it, not to solve it for you. I already gave you all the hints. So, good luck!
Reputation Points: 46
Solved Threads: 11
Junior Poster
orko is offline Offline
164 posts
since Apr 2006
Oct 24th, 2008
0

Re: need help regarding hangman like thing

Thanks a lot for ur help ..
i learnt a lot. i stayed awake till 12 last night trying to figure it out .. and today before i had to show it to the teacher ... i couldnt make it work as i wanted ..
replace in java replaces all characters which i never knew . and teacher realized becoz of my long talk with her that she had it wrong too .. she used insert which kept adding extra dots to the word. she said i should redo mine from scratch if i wanted to let it work .. she didnt have any other idea .. i got an 8 since i put lots of effort in it .. i will study the basics of java since i really suck at it ...
take care
Reputation Points: 10
Solved Threads: 0
Light Poster
Modo is offline Offline
35 posts
since Oct 2008
Oct 24th, 2008
0

Re: need help regarding hangman like thing

You DON'T suck at all! You are doing pretty good comparing to a lot of other new bies. I also didn't know about replace function for String that it replaces all characters, another work around would be to use the replace of StringBuffer. I am also going to school; so can only post when I have some free time. Your teacher is definitely wrong, cause you wouldn't have to do it from scratch. Every problem has different solutions. She might have different approach to solve it. So, good luck!
Reputation Points: 46
Solved Threads: 11
Junior Poster
orko is offline Offline
164 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: linear programming formulation
Next Thread in Java Forum Timeline: Compile time error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC