0

I have TextView with text that changed dynamically. i want tokenizing this text with delimiter space " " and send to another textview

this is my code

   public void onClick(View v) {
    // TODO Auto-generated method stub
    if (v.getId()==R.id.button5){
        Intent i = new Intent(this, Tokenizing.class);

        String test = ((TextView)findViewById(R.id.textView6)).getText().toString();
        String result = null;
        StringTokenizer st2 = new StringTokenizer(test," ");
            while (st2.hasMoreTokens()) {
                String st3 = st2.nextToken();
                System.out.println(st3);

                result = st3;
        }
        i.putExtra("result", result);
        startActivity(i);
        Log.i("Test Klik Next", result);

but i have just last word in the textview. text before tokenizing:

        Examples of paradoxes can be humorous and confusing

text after tokenizing:

        confusing

where is the part of my coding is wrong?

abooo
  • 15
  • 7
  • It's not *wrong* anywhere, it's doing what you told it to do. What do you expect the `result` variable to contain? At the moment it just contains the last word. – CodingIntrigue Jan 07 '14 at 09:24
  • Your `result` is getting updated in the loop each time. And will have the last word at the end. if you want to do something with each word, do so in the loop – rajesh Jan 07 '14 at 09:24

3 Answers3

4

You are overwriting the result every time you read a new token

result = st3;

So it's always equal to the last value. Change the type of result from String to StringBuilder and just build it as you go

 result.append(st3 + " "); //re-adding the space as the StringTokenizer will remove it

Then after the StringTokenizer loop just get the built String using result.toString()


Why not result += st3?

Some other answers suggest you do this. The reason not to do this is that in Java, String is immutable. This means they can't be changed so any time you append two String objects, a new String object is created. So every step through the loop you are creating a new String object which is inefficient and unnecessary. StringBuilder is not immutable and Strings can be appended to it without the overhead of creating new objects every time.

StringTokenizer

Worth noting -as @RGraham said in the comments- that this class is deprecated. This means it's no longer in common use, use of it is discouraged and it could be removed at some point.
More information here.

Tokens - in or out

As other answers assumed the opposite of me and after discussion on one of said answers and on meta, I feel I need to clarify this. I 'm not sure if your intention was to get rid of the tokens (in this case spaces " ") and end up with

Examplesofparadoxescanbehumorousandconfusing

or to replace them when outputting the final String and get out what you put in. So I assumed you wanted to preserve the original meaning and replace them. Otherwise a quicker way to get the result above would be to simply skip all the tokenizing and do

test.replaceAll(" ","");
Community
  • 1
  • 1
Ross Drew
  • 8,163
  • 2
  • 41
  • 53
  • 1
    May also be worth noting that StringTokenizer is a legacy class that is retained for compatibility reasons and its use is discouraged in new code – CodingIntrigue Jan 07 '14 at 09:36
  • replace `String result = null;` with `StringBuilder result = new StringBuilder();` and replace `result = st3` with the code I suggested in my post. Lastly, replace `i.putExtra("result", result);` with `i.putExtra("result", result.toString());` – Ross Drew Jan 07 '14 at 09:38
  • 1
    yeah work bro i try your code and i add result.append(st3+'\n'); and the result like this Examples of paradoxes can be humorous and confusing thanks bro, and next process i want to remove punctuation and get that text to lowercase. can you help me again? – abooo Jan 07 '14 at 09:58
  • Look at the methods of `String` called `toLowerCase` and `replaceAll` – Ross Drew Jan 07 '14 at 10:00
0
  while (st2.hasMoreTokens()) {
                    String st3 = st2.nextToken();
                    System.out.println(st3);

                    result = st3;  // Everytime you are reassigning result to some other String
            }
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
0

Replace result = st3; by (initialize String result=" ";) result+=st3+" "

and then replace i.putExtra("result", result); with i.putExtra("result", result.trim());

Try this , it will show perfect result.

You can also do this result.append(st3+" "); and then i.putExtra("result", result.trim());

Satyaki Mukherjee
  • 2,857
  • 1
  • 22
  • 26