I have a method that accepts a String array as an argument. When I pass it e.g. {"4","4","4"}, it works perfectly fine. However it gives a different result when I pass it "444".split("").
FSA A = generateFSA2();
String s = "444";
String[] sArr = {"4","4","4"};
String[] sArr2 = s.split("");
System.out.println(s.split("").length + ", " + sArr.length + ", " + sArr2.length);
for(int i = 0; i < sArr.length; i++) {
System.out.println("Strings equal = " + sArr[i].equals(sArr2[i]));
}
System.out.println("Accepted = " + isAccepted(A, s.split("")));
System.out.println("Accepted = " + isAccepted(A, sArr));
System.out.println("Accepted = " + isAccepted(A, sArr2));
Output:
3, 3, 3
Strings equal = true
Strings equal = true
Strings equal = true
Accepted = false
Accepted = true
Accepted = false
What's going on here?
EDIT: The isAccepted code:
public static Boolean isAccepted(FSA A, String[] w) {
return isAcceptedRec(A, w, 0, 0);
}
private static Boolean isAcceptedRec(FSA A, String[] w, int q, int i) {
if (i == w.length) {
for (int qF : A.finalStates)
if (q == qF)
return true;
return false;
}
for (Transition t : A.delta)
if (t.from == q && t.label == w[i])
if (isAcceptedRec(A, w, t.to, i + 1))
return true;
return false;
}
It's for testing FSAs. The code isn't mine, it is from an assessment, though obviously this isn't part of the assessment. I'm sticking to String arrays instead of splitting strings since apparently, that doesn't work.