The code below have some logic error. Here is the code import java.io.File; impo
ID: 3756018 • Letter: T
Question
The code below have some logic error.
Here is the code
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<StateTransition, Integer> stateTransitions = new HashMap<StateTransition, Integer>();
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<inputString.length(); i++)
{
currentState = stateTransitions.get(new StateTransition(currentState, inputString.charAt(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");
}
}
}
> run Validator
java.lang.ArrayIndexOutOfBoundsException: 0
at Validator.main(Validator.java:12)
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)
> run Validator statemachine.txt 112.txt
Testing with input string : aCb
Failure at position 1 found character C
112.txt has more than one line content. However this code only read and deal with first line. There should be some problem with the loop.
Explanation / Answer
mport 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<StateTransition, Integer> stateTransitions = new HashMap<StateTransition, Integer>();
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<inputString.length(); i++)
{
currentState = stateTransitions.get(new StateTransition(currentState, inputString.charAt(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");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.