StringTokenizer returns null instead of String -



StringTokenizer returns null instead of String -

the segment of code meant take line of text pulled text file, separate separate tokens, , store each token in index of array. @ first thought problem text file, putting string editor straight didn't prepare it.

with string such :

"chicken|none|beast|any|0|1|1|hey chicken!"

a stringtokenizer object delimiter | returns first 4 tokens proper strings, null remaining four.

interestingly enough, string :

"gnoll|none|general|any|2|2|2|taunt|taunt"

will homecoming first 5 tokens proper strings, null remaining 4 well.

if problem final 4 tokens, why stringtokenizer returning nulls in fashion?

code:

string[] parameter = new string[10]; string rawtxt = "chicken|none|beast|any|0|1|1|hey chicken!"; stringtokenizer t = new stringtokenizer(rawtxt, "|"); (int = 0; < t.counttokens(); i++) { parameter[i] = t.nexttoken(); system.out.print(parameter[i] + " "); }

output:

chicken none beast any

stringtokenizer deprecated should utilize string split method , works code

import java.util.arrays; public class mainclass { public static void main(string[] args) { string[] parameter = new string[10]; string rawtxt = "chicken|none|beast|any|0|1|1|hey chicken!"; string[] split = rawtxt.split("\\|"); system.out.println(arrays.tostring(split)); } }

string stringtokenizer

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -