| | |
need help regarding hangman like thing
![]() |
•
•
Join Date: Oct 2008
Posts: 35
Reputation:
Solved Threads: 0
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:
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)
import java.util.*; public class Scanner3 { public static void main(String args[]) { //Object aanmaken Scanner woordin= new Scanner(System.in); //Aanmaken variabele String gwoord; //printen van het tekst System.out.print("Geef een woord: "); //Woord in de variabele zetten gwoord=woordin.nextLine(); //String buffer puntjes wordt aangemaakt StringBuffer puntjes = new StringBuffer(); //Object letterin wordt aangemaakt van de klasse scanner Scanner letterin = new Scanner(System.in); //Variabele letter wordt aangemaakt String letter; //Printen van het tekst boolean Geraden = false; while(Geraden == false) { System.out.print("Geef een letter: "); //de letter wordet ingevoerd letter=letterin.nextLine(); //String puntje wordt aangemaakt en er wordt een puntje als karakter ingezet String puntje = "."; //herhalen tot de aantal letters vanhet in gevoerde woord for (int i = 0; i < gwoord.length( ); i++) { // Als het lettertje voorkomt op index i if( gwoord.charAt( i ) == letter.charAt( 0 ) ) { // Juiste letter invoegen puntjes.append(letter.charAt( 0 )); } else { // Anders het puntje plaatsen puntjes.append(puntje.charAt( 0 )); } } //Het puntjes woord wordt geprint op het scherm System.out.println("Geraden letters: "+ puntjes ); puntjes.delete(0,gwoord.length()); //if(gwoord.equals(totnugeraden)) //{ //Geraden = true; //} } } }
•
•
Join Date: Apr 2006
Posts: 164
Reputation:
Solved Threads: 10
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:
Try the following; you will get something better:
Java Syntax (Toggle Plain Text)
String puntje = "."; while(Geraden == false) { System.out.print("Geef een letter: "); //de letter wordet ingevoerd letter=letterin.nextLine(); //String puntje wordt aangemaakt en er wordt een puntje als karakter ingezet //herhalen tot de aantal letters vanhet in gevoerde woord for (int i = 0; i < gwoord.length( ); i++) { // Als het lettertje voorkomt op index i if( gwoord.charAt( i ) == letter.charAt( 0 ) ) { // Juiste letter invoegen puntjes.append(letter.charAt( 0 )); } else { // Anders het puntje plaatsen puntjes.append(puntje.charAt( 0 )); } } //Het puntjes woord wordt geprint op het scherm System.out.println("Geraden letters: "+ puntjes );
A Perfect World
•
•
Join Date: Oct 2008
Posts: 35
Reputation:
Solved Threads: 0
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 ..
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 ..
•
•
Join Date: Apr 2006
Posts: 164
Reputation:
Solved Threads: 10
Hey! You got me wrong. I didn't delete anything. I just moved the following line:
You also have to use replace function instead of append. It should be like this:
Java Syntax (Toggle Plain Text)
String puntje = ".";
Java Syntax (Toggle Plain Text)
String puntje = ""; for(int i=0; i<gwoord.length(); i++) { puntje.append("."); } 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 ) ) //I assume here character matches { //Here you were doing wrong: puntjes.replace(puntje.charAt(i), letter.charAt( 0 )); } else //I assume the character didn't match { //do nothing } } System.out.println("Geraden letters: "+ puntjes ); puntjes.delete(0,gwoord.length()); } }
A Perfect World
•
•
Join Date: Oct 2008
Posts: 35
Reputation:
Solved Threads: 0
thanks a lot Orko .. it compiles .. but the main problem is that it says:
String index out of range
this is my code now
String index out of range
this is my code now
Java Syntax (Toggle Plain Text)
import java.util.*; public class Scanner32 { public static void main(String args[]) { //Object aanmaken Scanner woordin= new Scanner(System.in); //Aanmaken van variabele String gwoord; //printen van de tekst System.out.print("Geef een woord: "); //Woord in de variabele zetten gwoord=woordin.nextLine(); //Stringbuffer puntjes wordt aangemaakt StringBuffer puntjes = new StringBuffer(); //Object letterin wordt aangemaakt van de klasse scanner Scanner letterin = new Scanner(System.in); //Variabele letter wordt aangemaakt String letter; //String puntje wordt aangemaakt en er wordt een puntje als karakter ingezet boolean Geraden = false; String puntje = "."; //for(int i=0; i<gwoord.length(); i++) { puntjes.append("."); } while(Geraden == false) { //Printen van het tekst System.out.print("Geef een letter: "); //de letter wordet ingevoerd letter=letterin.nextLine(); //herhalen tot de aantal letters vanhet in gevoerde woord for (int i = 0; i < gwoord.length( ); i++) { // Als het lettertje voorkomt op index i if( gwoord.charAt( i ) == letter.charAt( 0 ) ) { // Juiste letter invoegen puntje.replace(puntje.charAt(i), letter.charAt(0)); } else { // Anders het puntje plaatsen puntjes.append(puntje.charAt( 0 )); } } //Het puntjes woord wordt geprint op het scherm System.out.println("Geraden letters: "+ puntjes ); puntjes.delete(0,gwoord.length()); //if(gwoord.equals(totnugeraden)) //{ //Geraden = true; //} } } }
Last edited by Modo; Oct 23rd, 2008 at 4:21 pm.
•
•
Join Date: Apr 2006
Posts: 164
Reputation:
Solved Threads: 10
You got two variables "puntje[type: String]" and "puntjes[type
tringBuffer]".
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:
tringBuffer]".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.
A Perfect World
•
•
Join Date: Oct 2008
Posts: 35
Reputation:
Solved Threads: 0
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:
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 );
}
}
}•
•
Join Date: Oct 2008
Posts: 35
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Apr 2006
Posts: 164
Reputation:
Solved Threads: 10
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!
A Perfect World
![]() |
Similar Threads
- Writing a Hangman game (C++)
- Help with hangman tutorial. (C++)
- newbie in need of help for hangman game (Python)
- QBasic character counter (Legacy and Other Languages)
Other Threads in the Java Forum
- Previous Thread: linear programming formulation
- Next Thread: Compile time error
| Thread Tools | Search this Thread |
911 actionlistener addball addressbook android applet application apps array automation binary bluetooth businessintelligence button card character class client code collision component consumer crashcourse css csv database desktop eclipse ee error fractal free ftp game givemetehcodez graphics gui html image integration j2me japplet java javaarraylist javac javadoc javaee javafx javaprojects jni jpanel julia jvm linked linux loan mac method migrate mobile netbeans objects online oriented phone physics printf problem program programming project projects radio recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server service set sms software sort sql swing test textfield textfields threads time tree trolltech ubuntu update utility windows





