Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Loop to find and replace multiple instances of a char

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Loop to find and replace multiple instances of a char

    So im trying to create a hangman like game. Ill add to it later but right now I just need the basics.
    I'm trying to create a loop to find the position that a guessed letter is in then replace the appropriate asterisk with said letter.
    What I have so far lets me run it but it replaces all the asterisks if the letter is in the phrase.


    import javax.swing.*;
    public class GuessingGame
    {
    public static void main(String[]args)
    {
    //string and variable declerations
    String guessingWord = "Dota is awsome";
    String answer = "**** ** ******";
    String guessedLetter;
    char sub;
    int length;
    int i;

    //in case the string length comes into play
    length = guessingWord.length();

    //Prompting the user to guess letter or phrase
    guessedLetter = JOptionPane.showInputDialog(null, "Enter a letter or the full phrase\nif you think you know the answer");


    //assigning variable to be the number of the position of the particular asterisk of the answer string
    int a = guessingWord.indexOf(guessedLetter);

    //if guessed letter is part of the asnwer
    if(a != -1)
    {
    while(a >= 0)
    {
    sub = guessingWord.charAt(a);
    answer = answer.replace(answer.charAt(a), sub);
    a = guessingWord.indexOf(guessedLetter, ++a);

    }

    JOptionPane.showMessageDialog(null, "That letter is in the phrase!\n" + answer);

    }
    else
    JOptionPane.showMessageDialog(null, "That letter is not in the phrase");



    }
    }

    I had the code written in a way before that seemed like it worked but the GUI would crash after a correct letter guess was made.
    I know i have some unused char's in there, Im going to remove it later once I get this part of my code to work, I can focus on the rest.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Loop to find and replace multiple instances of a char

    I would recommend you start out by writing down a list of steps to take in solving the problem. This will greatly improve the chances of the code doing what you want it to do when you get to writing code. If you get stuck post your list of steps and your code with the question.

    Also please see the announcements page for the use of code tags.

Similar Threads

  1. Log location of multiple instances of jboss...
    By rathi in forum Java Servlet
    Replies: 2
    Last Post: January 23rd, 2012, 10:46 PM
  2. Replies: 0
    Last Post: January 17th, 2011, 05:14 AM
  3. Multiple class instances ??? But how ???
    By dumb_terminal in forum Object Oriented Programming
    Replies: 6
    Last Post: December 2nd, 2010, 08:42 AM
  4. Replies: 0
    Last Post: December 1st, 2010, 06:10 AM
  5. Multiple instances of linked list
    By thedolphin13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 11th, 2010, 07:48 PM