0

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...

  1. My first StringTokenizer displays what I need on the console but adds an unneeded extra "@" at the end.
  2. 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());


    }

}
Emmanuel Henri
  • 153
  • 3
  • 27
  • [StringTokenizer](http://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html) is [legacy class](http://stackoverflow.com/a/6983908/1393766). Why not just use String methods like `"Java Programming".replace(" ","~~~").replace('a','@')`? – Pshemo Jul 20 '13 at 14:54
  • Why are you using stringtokenizer? I don't see the need for that. You could just use .replace('a','@') – Rosdi Kasim Jul 20 '13 at 15:02
  • I wish I could use something else but it's for an assignment and we NEED to use StringTokenizer unfortunately. – Emmanuel Henri Jul 20 '13 at 15:08

3 Answers3

3

There are two major flaws in your code.

Firstly, if you observe methods encodeTokenA() closely,

 while (st.hasMoreTokens()){
          String token = st.nextToken();
          System.out.format(token + "~~~");
          }
          return token;

Here you formatted the block level String 'token' inside the loop but you are eventually returning the instance variable 'token'.

Secondly, between your two function calls encodeTokenA() and encodeTokenB() you are expecting the String object to be modified. Remember strings are immutable in java.

Ankur Shanbhag
  • 7,746
  • 2
  • 28
  • 38
  • So should I add both token replaced within one method to make this work? This is for an assignment therefore I need to use StringTokenizer to arrive at this.... – Emmanuel Henri Jul 20 '13 at 15:04
  • Firstly you should return the formatted value from the method encodeTokenA() and pass this formatted value to the next method(encodeTokenB()). However I am still not sure if your formatting logic will yield you expected results. But the things I have mentioned has to be done first. Then check your formatting logic. – Ankur Shanbhag Jul 20 '13 at 15:11
2

Why not just:

String whatever = "Java Programming."
    .replace('a', '@')
    .replaceAll("\\s", "~~~");
sjr
  • 9,769
  • 1
  • 25
  • 36
  • Remember that the method [replaceAll()](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html) takes a regexp as argument – reto Jul 20 '13 at 15:05
  • 1
    true, sometimes people just don't know these APIs exist though – sjr Jul 20 '13 at 15:14
0
  1. in your while loop in encodeTokenA, you are printing out the token + @ on every loop. This is why you will always have the 'extra @'.
  2. your two encode methods will only return the last token in your tokenized string. If you want to build a fully replaced string, you should instantiate a StringBuffer and append, then assign to your token.

You have also introduced a local scope var token, which will not modify your instance var token (mentioned by @Ankur)

@sjr has made this all moot by posting a concept that more concise using built-in methods

akf
  • 38,619
  • 8
  • 86
  • 96