I must use 2d arrays in my code and I am having a lot of trouble splitting the string in to a 1d array and storing it in a position in my 2d array testCases[][]. I am using the split function after I read in the string from the text file, to create an array of characters. Everything in my code below seems to make sense to me, however, when I try to iterate and print out the testCases array to ensure I collected the right data, it is printing out incorrect data.
I appreciate any help with figuring this one out, I have spent hours on this one problem.
Many thanks in advance.
//Read number of test cases
String x = fin.nextLine();
int numTests = Integer.parseInt(x);
//create array
testCases = new String[numTests][100];
//Read actual test cases and store in 2d array
for(int i = 0; i < numTests; i++){
String testCaseString = fin.nextLine();
//System.out.println(testCaseString);
testCases[i] = testCaseString.split("(?!^)");
System.out.println(testCases[i][0]);
}
for(int z=0; z < numTests; z++){
for(int q=0; q < z; q++){
System.out.printf("%s ", testCases[z][q]);
}
System.out.println();
}
Test Cases – Text file position
2
bcb
c
Current Console Output
c
Desired console Output
b c b
c
You have problems with outputting the data.
Try
In the inner loop you should iterate until
testCases[z].length.