I have two 4*4 matrices in JAVA, where one matrix holds observed counts and the other expected counts.
I need an automated way to calculate the p-value from the chi
I recommend to simply use a Java library that does a ChiSquare test for you. There are enough of them:
This is not a complete list, but what I found in 5 minutes searching.