2

I'm trying to remove every "x" regardless of case only from the first and second index but my program only removes the first index's letter and then loops without removing the second one.

    Scanner userIn = new Scanner(System.in);        
    StringBuilder str =  new StringBuilder();
    str.append(userIn.nextLine());

    for(int i = 0; i<2; i++) {
        if((str.charAt(i) == 'x') || (str.charAt(i) == 'X')) {
            str = str.deleteCharAt(i);
        }
    }
    System.out.println(str);
  • 1
    This is a perfect opportunity for you to learn the power debugging. Inspect the value of `str` and `str.charAt(i)` during each loop iteration. To make debugging easier remove the user input and hardcode the string at the beginning of the test. – kryger Sep 21 '18 at 17:43
  • Consider what happens if you delete a character at index 0: the character at index 1 is shifted to index 0... – Mark Rotteveel Sep 21 '18 at 17:43
  • [What does your step debugger tell you?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). Your question can be answered very quickly and easily with your step-debugger. You should always try and solve your problems with a step debugger before coming to StackOverflow. –  Sep 23 '18 at 15:28

4 Answers4

5

This is because when you delete a letter at the beginning all the characters shift. So for String str = "xxHello";:

x x H e l l o
0 1 2 3 4 5 6

Then when you delete the first x:

x H e l l o
0 1 2 3 4 5

So on your second iteration it will check to see if the first index (In this case H) is X. To fix this you can set i to 1 and then loop to zero:

StringBuilder str =  new StringBuilder();
str.append(userIn.nextLine());

for(int i = 1; i> -1; i--) {
    if((str.charAt(i) == 'x') || (str.charAt(i) == 'X')) {
        str = str.deleteCharAt(i);
    }
}
System.out.println(str);

Output:

Hello
GBlodgett
  • 12,704
  • 4
  • 31
  • 45
2

You could use str.delete(start, end) instead of the loop. For example:

String substr = str.substring(0,2).toLowerCase();
str.delete(
    substr.indexOf('x'),
    substr.lastIndexOf('x')+1
);
Rene Knop
  • 1,788
  • 3
  • 15
  • 27
1

Imagine that your input string is "XXY". Let's step through your code:

for(int i = 0; i<2; i++) {
    if((str.charAt(i) == 'x') || (str.charAt(i) == 'X')) {
        str = str.deleteCharAt(i);
    }
}

On the first iteration, i is equal to 0, so we check if the first character in the string is equal to 'x' or to 'X'. The first character of "XXY" is in fact equal, so we execute the contents of the if statement:

str = str.deleteCharAt(i);

Now our string is "XY". And now we go through the loop again.

On the second iteration, i is equal to 1, so we check the second character in the string. But now our string is "XY", so the second character is 'Y' and the if check fails.

Ben P.
  • 52,661
  • 6
  • 95
  • 123
0

Since there are only first two deletion to be done so inbuilt function can be used :

Scanner userIn = new Scanner(System.in);        
StringBuilder str =  new StringBuilder();
str.append(userIn.nextLine());
int j=0;
for(int i = 0; i<2; i++) {
    if((str.charAt(j) == 'x') || (str.charAt(j) == 'X')) {
        str = str.deleteCharAt(j);
    }else{
        j++;
    }
}
System.out.println(str);
urs_ng
  • 33
  • 11