How do I get this section of code to run until the tape is done?

149 Views Asked by At

So here is my code:

The purpose of it is to take in a single input tape, and the description of a turing machine and say whether or not the string was rejected. I have had no issues with loading either the input tape or the description of the turing machine.
My main issue comes when I am trying to get the machine to check more than just the first symbol in the input tape line. No matter how large I make the i in the for loop go to (i.e for (int i = 0; i < 10000; i++)) it always flips out and says "Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1". I tried recursion, while loops, and for loops. Perhaps I didn't have the right parameters. Tapem is an array list of all of the states in my turing machine. Sample Tape is this (1 0 x 2 >) - Basically, if it is at the first state, and the current bit on the input string is a 0, we replace the 0 with an x and change the current stage to 2. The > means the next slot on the input tape we look at is a to the right of our current bit on the input string. Thanks in advance!

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();
        }

    }
}
0

There are 0 best solutions below