Question: I was planning to construct a program that validates input using a state machine. I created a folder named ''statemachine'', and there is a text

I was planning to construct a program that validates input using a state machine. I created a folder named ''statemachine'', and there is a text file called statemachine.txt in it.

Then I used Drjava to run this program. Also, I saved program file in to statemachine folder. Could you please tell me why this program can not working.Here is two code below. I will show the result given by Drjava which might be helpful.

java.lang.ArrayIndexOutOfBoundsException: 0

at statemachine.Validator.main(Validator.java:23)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.lang.reflect.Method.invoke(Unknown Source)

at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:267)

Validator.java

package statemachine; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Scanner; class Validator { public static void main(String args[]) throws IOException { Scanner scanner = new Scanner(new File(args[0])); Map stateTransitions = new HashMap(); while(scanner.hasNextLine()) { stateTransitions.put(new StateTransition(scanner.nextInt(), scanner.next().toCharArray()[0]), scanner.nextInt()); } scanner = new Scanner(new File(args[1])); String inputString = scanner.next(); scanner.close();

Integer currentState = 0;

int i=0;

System.out.println("Testing with input string : " + inputString);

for(i=0; i

if(currentState == null) { System.out.println("Failure at position " + i + " found character " + inputString.charAt(i)); break; } else if(currentState == 999) { System.out.println("Success"); break; } } if(i == inputString.length()) { System.out.println("Input string ended before success transition"); } } }

package statemachine;

class StateTransition { int currentState; char transitionInput; public StateTransition(int currentState, char transitionInput) { this.currentState = currentState; this.transitionInput = transitionInput; } @Override public int hashCode() { return this.currentState + this.transitionInput; } @Override public boolean equals(Object obj) { StateTransition st = (StateTransition)obj; return (this.currentState == st.currentState && this.transitionInput == st.transitionInput); }}

package statemachine;

class StateTransition { int currentState; char transitionInput; public StateTransition(int currentState, char transitionInput) { this.currentState = currentState; this.transitionInput = transitionInput; } @Override public int hashCode() { return this.currentState + this.transitionInput; } @Override public boolean equals(Object obj) { StateTransition st = (StateTransition)obj; return (this.currentState == st.currentState && this.transitionInput == st.transitionInput); }}

StateTransition.java

package statemachine;

class StateTransition { int currentState; char transitionInput; public StateTransition(int currentState, char transitionInput) { this.currentState = currentState; this.transitionInput = transitionInput; } @Override public int hashCode() { return this.currentState + this.transitionInput; } @Override public boolean equals(Object obj) { StateTransition st = (StateTransition)obj; return (this.currentState == st.currentState && this.transitionInput == st.transitionInput); }}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!