2

I have this line

StringTokenizer tk=new StringTokenizer (b=10," =",true);

my output is

b
=
10

and this perfect but again if my line is this

StringTokenizer tk=new StringTokenizer (DEFI b=10," =",true);

And my output is

DEFI

b
=
10

I don't wanna include that empty o white space in the ST, How can I avoid including the white space cause I want this kind of output

DEFI
b
=
10
Baker1562
  • 337
  • 3
  • 20

2 Answers2

2

If you can, I suggest using String.split() method:

String[] tokens = "DEFI b=10".split("(\\s|(?=\\=)|(?<=\\=))");
olsli
  • 831
  • 6
  • 8
2

String tokenizer contrusts a string tokenizer from a specified string: (https://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html)

      public StringTokenizer(String str, String delim, boolean returnDelims)

As your parameters you have a a object declaration declaring an int value, a delim which is an equal sign and delim flag which is true (which returns the delim character as a token)

Maybe you should refactor your String tokenizer instance to the following:

     String b = "DDEFI-b-=-10"; //Create the string to be evaluated

     String delim = "-"; //Create a delim which serves as a separator

(Defining the string as "DEFI-b-=-10" - will provide the correct spacing with your delim)

     StringTokenizer tk= new StringTokenizer (b, delim);

    /* Loop through the elements printing out the specified string */

     while(tk.hasMoreElements()) {
          System.out.println(tk.nextToken());
       }



     //Result
     DEFI
     b
     =
     10  
23rharris
  • 256
  • 1
  • 8