Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

****************I have the following code in java for a astate machine but I\'m

ID: 3582731 • Letter: #

Question

****************I have the following code in java for a astate machine but I'm getting an error when running it.....PLZ FIX ERROR(THERE IS ALSO TEXT FILE I WILL COPY AND PAste on the bottom)

import java.io.*;

import java.lang.*;

import java.util.*;

public class DFA {

   private State start= null;
   private State currentState = null;
   private HashMap<String, State> stateMap;
   public DFA() {
stateMap = new HashMap<String, State>();
readData();
   }

   private void readData() {
java.io.File file = new java.io.File("DFARules.txt");
try {
   Scanner input = new Scanner(file);
   while (input.hasNext()) {
  
String stateKey;
String stateInput;
String transitionState;
  
String cord = input.nextLine();
String[] stateArgs = cord.split(" ");
  
if (stateArgs[0].equals("start")) {
   stateKey = stateArgs[1];
   State state = stateMap.get(stateKey);
}
else if (stateArgs[0].equals("end")) {
   stateKey = stateArgs[1];
   State state = stateMap.get(stateKey);
   state.setStateType(StateType.START);
}
else{
   stateKey = stateArgs[0];
   stateInput = stateArgs[1];
   transitionState = stateArgs[2];
   this.addState(stateKey, stateInput, transitionState);
}
  
   }
   input.close();
}
catch (FileNotFoundException e) {
   System.out.println("No State Machine rules found. "
   + "Please look at the instructions and try again.");
}
   }

   public void addState(String stateKey, String input, String transitionState) {
State state = stateMap.get(stateKey);
if (state == null) {
   state = new State(stateKey);
}
state.addInput(input, transitionState);
   }

   public void runDFA(String inputString) {
  
start = new State(""+inputString.charAt(0));
  
   if (start != null) {
   currentState = start;
   System.out.println("Start " + currentState.getStateKey());
}
else {
   System.out.println("DFA not well defined");
}
  
for (int i = 1; i < inputString.length(); i++) {
   String input = inputString.substring(i - 1, i);
   String transitionState = currentState.getTransitionState(input);
   currentState = stateMap.get(transitionState);
   System.out.println("input: " + input + "Next State: "
   + currentState.getStateKey());
   if (currentState.getStateType().equals(StateType.END)) {
System.out.println("End");
break;
   }
   }
  
   }

   public enum StateType {
START, END, TRANSITION
   }

   public class State {
  
private String stateKey;
private HashMap<String, String> inputMap;
private StateType stateType;
  
public State(String stateKey) {
   this.stateKey = stateKey;
   inputMap = new HashMap<String, String>();
}
  
public void addInput(String input, String transitionState) {
inputMap.put(input, transitionState);
}
  
public String getTransitionState(String transitionState) {
   return inputMap.get(transitionState);
}
  
public String getStateKey() {
   return stateKey;
}
  
public void setStateKey(String stateKey) {
   this.stateKey = stateKey;
}
  
public HashMap<String, String> getInputMap() {
   return inputMap;
}
  
public void setInputMap(HashMap<String, String> inputMap) {
   this.inputMap = inputMap;
}
  
public StateType getStateType() {
   return stateType;
   }
  
public void setStateType(StateType stateType) {
   this.stateType = stateType;
}
  
   }

   public static void main(String[] args) throws Exception
   {
  
String input ;
  
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  
System.out.println("Enter String:");
  
input = br.readLine();
  
DFA dfa = new DFA();
dfa.runDFA(input);
   }

}

////////////////////////////////////////////text file///////////////////

0 a 1
0 b 0
1 a 1
1 b 2
2 a 2
2 b 2
start 0
final 2


Explanation / Answer

Please update your imports. Instead of using

import java.lang.*;

Use specific imports.

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.InputStreamReader;

import java.util.HashMap;

import java.util.Scanner;

In main method, when you decalre String input;, please initialize it. String input = ""; or String input = null;