Looking for some help with string builder
Have an assignment to have the user enter a string, then remove the vowels.
The only stipulation is to use string builder, but i seem to have lost my notes on it.
I cobbled this together, but it isn't working, problem is, i'm not sure why because my understanding of string builder, and removing characters, is pretty weak.
I know the big list of if statements is...verbose, but i looked at a classmates code (He has quite a bit more java experience then me) and he used case statements to cut it down, but i didn't want my code to copy his, so i went with this format.
Any tips would be appreciated.
import javax.swing.*;
publicclass J7E3{
publicstaticvoid main (String arg[])
{
String snum;
snum=JOptionPane.showInputDialog("Enter A string you don't like the vowels in ");
StringBuilder sb=new StringBuilder(snum);
for (int x=0; x < sb.length(); x++)
if (aCharacter =='a')
stringsb.deleteCharAt(stringsb.indexof("a"));
if (aCharacter =='e')
stringsb.deleteCharAt(stringsb.indexof("e"));
if (aCharacter =='i')
stringsb.deleteCharAt(stringsb.indexof("i"));
if (aCharacter =='o')
stringsb.deleteCharAt(stringsb.indexof("o"));
if (aCharacter =='u')
stringsb.deleteCharAt(stringsb.indexof("u"));
if (aCharacter =='A')
stringsb.deleteCharAt(stringsb.indexof("A"));
if (aCharacter =='E')
stringsb.deleteCharAt(stringsb.indexof("E"));
if (aCharacter =='I')
stringsb.deleteCharAt(stringsb.indexof("I"));
if (aCharacter =='O')
stringsb.deleteCharAt(stringsb.indexof("O"));
if (aCharacter =='U')
stringsb.deleteCharAt(stringsb.indexof("U"));
System.out.print("Here is your string without pesky vowels: "+sb.toString());
}
}
[3175 byte] By [
Alexmina] at [2007-11-27 3:34:13]

Ok, i can get it to compile and run now, it just doesn't do what i ask, in that it doesn't remove the vowels.
I initialized acharacter, and saw that i had indexof instead of indexOf.
So now, it asks for input, it shows the input, but thats it.
Heres the updated code.
import javax.swing.*;
public class J7E3 {
public static void main (String arg[])
{
String snum;
int aCharacter=0;
snum=JOptionPane.showInputDialog("Enter A string you don't like the vowels in ");
StringBuilder sb= new StringBuilder(snum);
for (int x=0; x < sb.length(); x++)
if (aCharacter =='a')
sb.deleteCharAt(sb.indexOf("a"));
if (aCharacter == 'e')
sb.deleteCharAt(sb.indexOf("e"));
if (aCharacter == 'i')
sb.deleteCharAt(sb.indexOf("i"));
if (aCharacter == 'o')
sb.deleteCharAt(sb.indexOf("o"));
if (aCharacter == 'u')
sb.deleteCharAt(sb.indexOf("u"));
if (aCharacter == 'A')
sb.deleteCharAt(sb.indexOf("A"));
if (aCharacter == 'E')
sb.deleteCharAt(sb.indexOf("E"));
if (aCharacter == 'I')
sb.deleteCharAt(sb.indexOf("I"));
if (aCharacter == 'O')
sb.deleteCharAt(sb.indexOf("O"));
if (aCharacter == 'U')
sb.deleteCharAt(sb.indexOf("U"));
System.out.print("Here is your string without pesky vowels: "+sb.toString());
}
}
Please think about what you are doing.
If you are looking for a character, why is aCharacter declared as an int?
You set aCharacter to 0, then never update it, yet you compare it to a different vowel character each time. Do you expect something different to happen even though aCharacter is 0 all the time?
You loop through the characters in the StringBuilder, but you never do anything with the position 'x'.
Each time you think you have found a character, you then do an indexOf to find the character again.
I doubt you want to print the string without the vowels *inside* the for loop.
Yeah, sorry, i've been up studying for another final.
But, to show you i'm not a complete idiot, i came up with this.
public class J7E3 {
public static void main (String arg[])
{
String snum;
snum=JOptionPane.showInputDialog("Enter A string you don't like the vowels in ");
StringBuilder sb= new StringBuilder(snum);
for (int x=0; x < sb.length(); x++)
{
char aCharacter;
aCharacter = sb.charAt(x);
if (aCharacter =='a')
sb.deleteCharAt(sb.indexOf("a"));
if (aCharacter == 'e')
sb.deleteCharAt(sb.indexOf("e"));
if (aCharacter == 'i')
sb.deleteCharAt(sb.indexOf("i"));
if (aCharacter == 'o')
sb.deleteCharAt(sb.indexOf("o"));
if (aCharacter == 'u')
sb.deleteCharAt(sb.indexOf("u"));
if (aCharacter == 'A')
sb.deleteCharAt(sb.indexOf("A"));
if (aCharacter == 'E')
sb.deleteCharAt(sb.indexOf("E"));
if (aCharacter == 'I')
sb.deleteCharAt(sb.indexOf("I"));
if (aCharacter == 'O')
sb.deleteCharAt(sb.indexOf("O"));
if (aCharacter == 'U')
sb.deleteCharAt(sb.indexOf("U"));
}
System.out.print("Here is your string without pesky vowels: "+sb.toString());
}
}
Which works.
I think i declared acharacter as an int because this is literaly the first time i've used a char the entire semester, and i'm so use to using ints i just sorta slipped it in.