所以这是我的代码:
它的目的是接收单个输入磁带,以及图灵机的描述并说明该字符串是否被拒绝。我没有加载输入磁带或图灵机的描述 当我试图让机器检查输入磁带线中的第一个符号时,我的主要问题就出现了。无论我在for循环中使用多大(即(int i = 0; i< 10000; i ++)),它总是翻转并说“线程中的异常”主“java.lang.IndexOutOfBoundsException:指数:1,大小:1“。 我尝试了递归,循环和循环。也许我没有合适的参数。 Tapem是我的图灵机中所有状态的数组列表。样本磁带是这个(1 0 x 2>) - 基本上,如果它处于第一个状态,并且输入字符串上的当前位是0,我们用x替换0并将当前阶段更改为2。 >意味着我们看到的输入磁带上的下一个插槽位于输入字符串上当前位的右侧。提前谢谢!
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JOptionPane;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author lindsua
*/
public class TuringMachine {
//move the head of the turing machine depending on the direction given
public static void move(char[] chary, String direct) {
for (int i = 0; i < chary.length; i++) {
if (direct == ">") {
chary[i] = chary[i++]; //move right
} else if (direct == "<") {
chary[i] = chary[i--]; //move left
}
}
}
public static void main(String[] args) throws IOException {
String allowed;
int startState;
int reject = 0;
String input;
File file = new File("TMM1.txt");
input = JOptionPane.showInputDialog(null, "Please enter the tape: ");
try {
Scanner scanner = new Scanner(file);
if (file.exists()) {
//The first line lists the number of states.
String s = scanner.nextLine();
s = s.substring(0, 1);
// line 2 = symbols that can appear in tape
allowed = scanner.nextLine();
for (int j = 0; j < input.length() - 1; j++) {
//If the sentence has any characters that are not allowed, it gains one violation.
if (!allowed.contains(Character.toString(input.charAt(j)))) {
reject++;
}
}
// line 3 is the start state
String t = scanner.nextLine();
char v = t.charAt(0);
startState = Integer.parseInt(Character.toString(v));
// line 4 final states
String u = scanner.nextLine();
u = u.replaceAll("\\s+$", "");
String[] a = u.split(" ");
for (int w = 0; w < a.length; w++) {
int q = Integer.parseInt(a[w]);
}
// currentState, Symbol we have, go to this state, move in this direction
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
int stateo = Integer.parseInt(line.substring(0, 1));
char tapesym = line.charAt(1);
char replacemt = line.charAt(2);
int staten = Integer.parseInt(line.substring(3, 4));
String direct = line.substring(4, 5);
Tape tape = new Tape(stateo, tapesym, replacemt, staten, direct);
ArrayList<Tape> tapem = new ArrayList<Tape>();
tapem.add(tape);
int current = 1;
int size = 100000;
char[] charizard = input.toCharArray();
for (int i = 0;i < tapem.size(); i++) {
// while(tapem.get(i).start != 9){
if ((current == tapem.get(i).start) && (tapem.get(i).curr == charizard[i])) {
charizard[i] = tapem.get(i).rep;
current = tapem.get(i).newS;
move(charizard, tapem.get(i).dir);
System.out.println(charizard);
System.out.println(current);
}
else if ((current != tapem.get(i).start || (tapem.get(i).curr != charizard[i]))){
i++;
}
// }
if (direct == ">" && (i <= charizard.length)) {
reject++;
}
if (direct == "<" && (i < 0)) {
reject++;
}
}
}
}
if (reject > 0) {
System.out.println("The input was rejected");
} else {
System.out.println("The input was accepted.");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}