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:

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;   
        //}
    }
    }
}

Recommended Answers

All 10 Replies

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:

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 );

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 ..

Hey! You got me wrong. I didn't delete anything. I just moved the following line:

String puntje = ".";

You also have to use replace function instead of append. It should be like this:

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());
      }
    }

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

String index out of range

this is my code now

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;   
        //}
    }
    }
}

You got two variables "puntje[type: String]" and "puntjes[type:StringBuffer]".
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());
    
       }

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 );
      }
    }
}

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!

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

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!

thanks again for ur help..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.