stringtokenizer - trying to print out the array in java -


system.out.println("please input elements , seperate each  comma."); e = dk.nextline(); string[] elems = new string[e.length()]; st = new stringtokenizer(e,",");  (int = 0; i<e.length(); i++) {    elems[i] = st.nexttoken().tostring(); }  (int i=0; i<e.length(); i++){    system.out.println(elems[i]); } 

i trying print out array elems[] wont work error java.util.nosuchelementexception @ java.util.stringtokenizer.nexttoken(stringtokenizer.java:349 seems @ line:

elems[i] = st.nexttoken().tostring();

can me identify , understand problem?

a correct version:

string[] elems = e.split(","); for(string elem : elems) {     system.out.println(elem); } 

the mistake made e.length() returns size of string (its number of characters) ended calling st.nexttoken() more times there actual tokens separated ",". hence exception.


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -