/** * Name: Erica Eddy * Course: CSCI 145/241 - Computer Science I * Section: 00_ * Assignment Number: ?? * * Project/Class Description * This contains an examples illustrating how to fix * looping problems with StringBuilders. * -- Eliminating selected vowels from a StringBuilder. * * Known Bugs: you get to fix this one! */ import java.io.*; public class StringBuilderLoopDelete { public static void main (String [] args) throws IOException { // This code is supposed to pull all occurrences of // 'a', 'e' and 'o' out of the StringBuilder // It doesn't work; positions of characters change // during the loop due to deleteCharAt(). StringBuilder sb = new StringBuilder("season"); for (int j = 0; j < sb.length(); j++) { char ch = sb.charAt(j); System.out.println("ch = " + ch); if (ch == 'a' || ch == 'e' || ch == 'o') sb.deleteCharAt(j); } System.out.println(sb.toString()); } } // How to fix: use a while loop, adjusting j's value // only when a character is NOT deleted