0

I am trying to replace letters in a string at specific locations. I am aware that there are many questions such as this already however I am still getting into trouble.

example: hiddenWord = "----" from my loop I find that at location 1 and 3 I would like to replace "-" with "a". So that hiddenWord now = "-a-a".

Main snip:

        btnA = new JButton("A");
    btnA.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            count += 1;
            lblTries.setText(count + " Tries"); 
            int i;
            String newName="";
            if (wordList[num].indexOf('a') > 0){
                System.out.print("Has A: ");
                for (i = -1; (i = wordList[num].indexOf("a", i + 1)) != -1; ) {
                    //System.out.print(i + " ,");

                    newName = hiddenWord.substring(0,i)+'a'+hiddenWord.substring(5);
                }                   
            }
            System.out.println(newName);
        }
    });

Please let me know if there are any other conventions that I should be doing differently..as you can tell I am very new to this.

Edit:

someone_somewhere has helped me see my error. My new code looks as followed

                if (wordList[num].indexOf('a') >= 0){
                for (int i = -1; (i = wordList[num].indexOf("a", i + 1)) != -1; ) {
                    hiddenWord = putCharAtPlaces(hiddenWord,'a',new int[]{i});
                    lblWordDisplay.setText(hiddenWord); 
                    System.out.println(i);
                }
Alex
  • 7
  • 3
  • 1
    Possible duplicate of [Replace a character at a specific index in a string?](http://stackoverflow.com/questions/6952363/replace-a-character-at-a-specific-index-in-a-string) – Tom Jan 10 '16 at 18:41

2 Answers2

0

You could use substring, but a simpler approach is to use StringBuilder

StringBuilder sb = new StringBuilder("----");

sb.setCharAt(1, 'a');
sb.setCharAt(3, 'a');

String s= sb.toString(); // -a-a

I would set the StringBuilder once and keep adding letters to it.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

Try something like this:

    public static void main(String args[]){
        String word = "----------";
        word = putCharAtPlaces(word,'a',new int[]{0,2,3});
        System.out.println(word);
    }

    private static String putCharAtPlaces(String word,char c, int[] is) {
        StringBuilder stringBuilder = new StringBuilder(word);
        for(int place:is){
            stringBuilder.setCharAt(place, c);
        }
        return stringBuilder.toString();
    }

You put the char setting under btnK. About the first char not working firs your btnJ should be:

btnJ.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        count += 1;
        lblTries.setText(count + " Tries");
        if (wordList[num].indexOf('j') >= 0) {//notice this is >= 0 to get the first char to work
            for (int i = -1; (i = wordList[num].indexOf("j", i + 1)) != -1;){
                hiddenWord = putCharAtPlaces(hiddenWord, 'j',
                        new int[] { i });
                lblWordDisplay.setText(hiddenWord);
                System.out.println(i);
            }
        }
    }
});
  • This works well for the most part but I am still running into a problem! The first letter is not being counted. I am using a modified version of your main method, Im trying to figure out how to post my new modified code. – Alex Jan 10 '16 at 21:00
  • You should be able to edit your question. So just add the new code at the end of the existing question. Like this I am not sure what the problem you are encountering is. – someone_somewhere Jan 10 '16 at 21:07
  • I added more info in the original question. Thank you for taking the time to help me. – Alex Jan 10 '16 at 21:10
  • I learned my error. Your original answer has helped me greatly. Thank you. – Alex Jan 10 '16 at 21:24
  • Think about creating a button class which would get the char it represents in the constructor because all of your buttons representing letters have practically the same code. Good luck with your programming ;) – someone_somewhere Jan 10 '16 at 21:27
  • This is my next step :) I need to review some tutorials but I think it shouldn't be too hard. – Alex Jan 10 '16 at 21:31