Note: I have to use StringTokenizer for this program (it's for an assignment)
I'm trying to get this string "Java Programming." to say "J@v@~~~Progr@mming." with StringTokenizer.
I have 2 problems with the following code...
- My first StringTokenizer displays what I need on the console but adds an unneeded extra "@" at the end.
- It doesn't grab my string token as it is "J@v@ Progr@mming." to apply the second StringTokenizer which would add "~~~" where there is a space.
What I am missing? I went through Java's API docs and couldn't figure it out.
import java.util.StringTokenizer;
public class StringToken {
private String token;
//Constructor with default text
public StringToken() {
token = "Java Programming.";
}
//Constructor with custom text
public StringToken(String newToken) {
token = newToken;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public String encodeTokenA(String newToken){
StringTokenizer st = new StringTokenizer(newToken, "a");
while (st.hasMoreTokens()){
String token = st.nextToken();
System.out.format(token + "@");
}
return token;
}
public String encodeTokenB(String newToken){
StringTokenizer st = new StringTokenizer(newToken, " ");
while (st.hasMoreTokens()){
String token = st.nextToken();
System.out.format(token + "~~~");
}
return token;
}
}
public class TestStringToken {
public static void main(String[] args) {
StringToken test = new StringToken();
test.encodeTokenA(test.getToken());
test.encodeTokenB(test.getToken());
System.out.println(test.getToken());
}
}