Java Array Parsing -
lets have string array called ar1, ar1[x] "harry potter"
now lets have string array called ar2, want ar2[x] equal "harry". how this?
here tried, did not work.
string ar2[] = new string[10]; int x = 0; while(x<9){ ar2[x] = ar1[x].split(" ").tostring(); x++; system.out.println(ar2[x]);}}}
the out put 9 null's.
it looks you're calling tostring()
on string array. 'split()' method returns string array, , want first element in array resulting split.
it looks want this:
string ar2[] = new string[ar1.length]; //better if not hard coded 10 int x = 0; while(x < ar1.length){ string[] temp = ar1[x].split(" "); ar2[x] = temp[0]; x++; //moved in initial edit fix null printing } //moved printing code out of loop populating array occurs (int = 0; < ar2.length; i++){ system.out.println(ar2[i]); }
Comments
Post a Comment