-2

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.

Example case: input: ?ababa?b? output: babababba

input: ababb?b output:ababbab

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

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

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




        }
hamster
  • 29
  • 6

1 Answers1

2

You have to assign the result to your variable, because String in java is immutable :

s = s.replace("?", "a");
^^^---------------------------assign the result to your variable
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140