I have a code in java for DFA simulation and I receive this error for loop below saying "i cannot be resolved to a variable". I dunno what is wrong with it, can any help me solve this problem? I tried the fixed suggestion on the ecllipse but another error would be shown.
import java.util.Scanner;
public class DFA {
public static void main(String[] args) {
Scanner sc;
sc = new Scanner (System.in);
int states;
System.out.println("Input Number of States: ");
states = sc.nextInt();
System.out.println("Input Number of Transition States: ");
int tStates = sc.nextInt();
int q [][] = new int [states] [tStates];
System.out.println("Input the Transition Table");
for (int i = 0; i < states; i++) {
System.out.println("State " +(i));
for(int j = 0; j < tStates; j++ ) {
q[i][j] = sc.nextInt();
}
}
System.out.println("Input the String: ");
String state1;
state1 = sc.next();
String in[] = state1.split("");
System.out.println("Transition Table ");
System.out.println(" 0 1");
for (int i = 0; i < states; i++) {
System.out.println("State " + (i) + " ");
for(int j = 0; j < tStates; j++ ) {
System.out.println("q" + q[i][j] + " ");
}
System.out.println("");
}
//input
int input [] = new int [in.length];
//splitting with space
for (int i = 0; i <= in.length; i++); {
if (in[i].equals("a")) {
input[i] = 0;
}
if (in[i].equals("b")) {
input[i] = 1;
}
}
System.out.println("_________________________");
int initial = 0;
int fin = (states - 1);
int current = initial;
int ip, nextstate;
for (int i = 0; i < in.length; i++) {
System.out.println("q" + current + "--" + input[i] + "-->");
ip = input[1];
nextstate = q[current][ip];
current = nextstate;
if (i == (in.length - 1)) {
System.out.println("q" + current);
}
}
if (current == fin) {
System.out.println("\nAccepted");
} else {
System.out.println("\nRejected");
}
}
}
This lines right here after the if the statement has the problems. The i that is enclosed in brackets is the one who is making errors.
for (int i = 0; i <= in.length; i++); {
if (in[i].equals("a")) {
input[i] = 0;
}
if (in[i].equals("b")) {
input[i] = 1;
}
}
It's the semicolon after the right parenthesis of the for statement: the for statement only contains an empty statement.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.