简体   繁体   中英

While loop does not exit

While trying to write a simple java code to replace a particular character in a given string by using loop I have been much confused to find the mistake. Details are as below:

INPUT String : "123qq11 1q1 11q1 1qq11 1q1 11q1"


REQUIRED OUTPUT: "123QQ11 1Q1 11Q1 1QQ11 1Q1 11Q1"


GOT OUTPUT:   "Q23QQ11 1Q1 11Q1 1QQ11 1Q1 11Q1" as infinite loop

ALGORITHM: Replacing 'q' by 'Q' in given String.

My code to be corrected is:

public class Rani {

    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("123qq11 1q1 11q1 1qq11 1q1 11q1");
        int j = 0;
        int i = 0;
        while (i < sb.length()) {
            while (i + 1 < sb.length()) { // while 2nd
                i++;

                if (sb.charAt(i) == 'q') {
                    j = i;
                    break;
                } else {
                    break;
                }
            }

            sb.replace(j, j + 1, "Q");

            System.out.println(sb);
        }
    }
}

Being new comer to java and programming I have been failed to manage the corrections.

Firstly you don't need 2 while loops.

Secondly i stays at sb.length() - 1 forever making it an infinite loop.

You could use a replace or replaceAll instead.

String st = "123qq11 1q1 11q1 1qq11 1q1 11q1"
st = st.replace("q", "Q");

If you want to do it with while loops.

int i = 0; 
while(i < sb.length())
{
 if(sb.charAt(i) == 'q')
  sb.setCharAt(i, 'Q');
i++;
}

Here is the working code

 public static void main(String[] args) {
    StringBuilder sb = new StringBuilder("123qq11 1q1 11q1 1qq11 1q1 11q1");
    int j = 0;
    int i = 0;
    while (i < sb.length()) {
        while (i < sb.length()) { // while 2nd

            if (sb.charAt(i) == 'q') {
                j = i;
                break;
            }
            i++;
        }

        sb.replace(j, j + 1, "Q");
        System.out.println(sb);
    }
}

Output: 123QQ11 1Q1 11Q1 1QQ11 1Q1 11Q1

If you don't want to use replace you can just do this.

for (int i = 0; i < st.length(); i++)
    if (st.charAt(i) == 'q')
        st.setCharAt(i, 'Q');
public static void main (String[] args) {
         StringBuilder sb = new StringBuilder("123qq11 1q1 11q1 1qq11 1q1 11q1");
         String strnew = "";
         for(int i=0;i<sb.length();i++)
         {
             if(sb.charAt(i)=='q')
             {      
                 strnew=strnew+"Q";

             }
             else
                 strnew=strnew+sb.charAt(i);
         }

         System.out.println(strnew);
    }

in your code you are telling i to increment too soon. in the inner loop i only gets to 30 while the length is actually 31. if you move the i++ to the end of the first loop but our side of the inner loop it should work they way you want. like so:

...
if (sb.charAt(i) == 'q') {
                j = i;
                break;
            }
            i++;
        }
...

the only problem with that is you are always replacing the first character of the string. the first number "1" is being replaced with a "Q". becasue you are using j to replace "q" it is always the character at the 0th slot.

You could even make it simpler buy just using one loop:

while (i<sb.length()){
    if (sb.charAt(i) == 'q'){
        sb.replace(i, i + 1, "Q");
    }
    i++;
}
System.out.println(sb);

or you could use a for loop:

for (i =0; i<sb.length();i++){
    if (sb.charAt(i)=='q'){
        sb.replace(i, i+1, "Q");
    }       
}
System.out.println(sb);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM