Java - Input string into array -
i have text file :
testcasez : a, b, c
testcasex : b,d
testcasec : b
i want have string array :
abcd
testcasez 1 1 1 0
testcasex 0 1 0 1
testcasec 0 1 0 0
public class teststring { public static void main(string[] args){ //need configure accordingly int row=3+1; int col=4+1; string[][] array=new string[row][col]; //construct string number of row-1 string s1="testcasez : a,b,c"; string s2="testcasex : b,d"; string s3="testcasec : b"; string[] strings=new string[]{s1,s2,s3};//strings should have lenghth of row-1 string[] allvalues=new string[]{"a","b","c","d"}; //polulate first row array[0][1]="a"; array[0][2]="b"; array[0][3]="c"; array[0][4]="d"; //above, can dynamically construct 2 arrays based on input(say files) for(int i=1;i<array.length;i++){//for every rest rows string text=strings[i-1]; string thecase=text.substring(0,text.indexof(":")).trim();// string valuetext=text.replaceall(thecase, "").replacefirst(":", "").trim(); array[i][0]=thecase; string[] values=valuetext.split(","); list valuelist=arrays.aslist(values); for(int j=1;j<array[i].length;j++){//for every cols if(valuelist.contains(allvalues[j-1])){//if contains value, array[i][j]="1"; } else array[i][j]="0"; } } //test output system.out.println(arrays.deeptostring(array)); } }
output like:
[[null, a, b, c, d], [testcasez, 1, 1, 1, 0], [testcasex, 0, 1, 0, 1], [testcasec, 0, 1, 0, 0]]
Comments
Post a Comment