String method not giving the right output : <p>So I was solving a question from a coding website that required me to replace ? with an a or b provided there are no two consecutive a... I have tried a lot and this is the code that i have written. although the code seems correct to my knowledge, whenever I run this code the output is same as the input itself. No changes are made to it.</p>

<p>Example case:
<strong>input</strong>:   ?ababa?b?
<strong>output</strong>:  babababba</p>

<p><strong>input</strong>: ababb?b
<strong>output</strong>:ababbab</p>

<p>The input has to be wither a,b or ?
 The output must be such that it has the highest precedence in dictionary.</p>

<p>However whatever input i give i get the same output. If I give ?ab as input I get the same as output please help me</p>

<pre><code>  package Beginner;

    import java.util.Scanner;

    public class ExplRuin {

        public static void main(String args[]){

            String s;

            Scanner in = new Scanner(System.in);

            s = in.nextLine();

            if(s.length()==1){

                if(s.equals("?"))
                    s.replace("?", "a");

            }   else
            {
                if(s.toString().startsWith("?")){
                    if(s.contains("?b"))
                        s.replace("?b","ab");
                    else
                        if(s.contains("?a"))
-                           s.replace("?a", "ba");  
                }
                if(s.endsWith("?")){
                    if(s.contains("a?"))
                        s.replace("a?", "ab");
                    else
                        if(s.contains("b?"))
                            s.replace("b?","ba");
                }
                if(s.contains("?a")||s.contains("a?")){
                    s.replace("?", "a");
                }
                else{
                    s.replace("?", "a");
                }       
            }
            System.out.print(s);
            }




        }
</code></pre>
