Why am i getting this error

Input string:
Exception in thread "main" java.util.NoSuchElementException: No line foundat java.base/java.util.Scanner.nextLine(Scanner.java:1651)at TMSimulator.main(TMSimulator.java:15)

import java.util.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator; 
import java.util.Scanner;
import java.io.FileReader;

public class TMSimulator {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.println("Input string: ");
        String inputString = input.nextLine();

        int numStates = 0;
        int haltingState = 0;
        int[][] transitionTable = {
         { 0, 0, 0 },
         { 0, 0, 0 },
         { 0, 0, 0 }
      };
      

        try {

            File file = new File("TM.txt");
            Scanner sc = new Scanner(file);

            numStates = Integer.parseInt(sc.nextLine());
            haltingState = Integer.parseInt(sc.nextLine());

            transitionTable = new int[numStates][256];

            for (int i = 0; i < numStates; i++) {
                for (int j = 0; j < 256; j++) {
                    transitionTable[i][j] = -1;
                }
            }

            while (sc.hasNextLine()) {
                String line = sc.nextLine();

                String[] parts = line.split(" ");

                int state = Integer.parseInt(parts[0]);
                int character = Integer.parseInt(parts[1]);
                int writeCharacter = Integer.parseInt(parts[2]);
                String direction = parts[3];
                int nextState = Integer.parseInt(parts[4]);

                transitionTable[state][character] = writeCharacter;

                if (direction.equals("R")) {
                    transitionTable[state][character + 128] = nextState + 128;
                } else if (direction.equals("L")) {
                    transitionTable[state][character + 128] = nextState;
                } else {
                    transitionTable[state][character + 128] = nextState;
                }
            }

            sc.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

        int state = 0;
        int head = 0;

        System.out.println(state + " " + inputString);

        while (state != haltingState) {

            int character = inputString.charAt(head);
            int writeCharacter = transitionTable[state][character];

            if (writeCharacter == -1) {
                System.out.println("Invalid input string");
                return;
            }

            inputString = inputString.substring(0, head) + (char) writeCharacter + inputString.substring(head + 1);

            if (writeCharacter >= 128) {
                state = writeCharacter - 128;
            } else {
                state = 0;
            }

            if (writeCharacter >= 0 && writeCharacter < 128) {
                head++;
            } else if (writeCharacter >= 128 && writeCharacter < 256) {
                head--;
            }

            if (head == -1) {
                inputString = "_" + inputString;
                head++;
            }

            if (head == inputString.length()) {
                inputString = inputString + "_";
            }

            System.out.println(state + " " + inputString);

        }
    }
}

The code gives this error

Input string:
Exception in thread "main" java.util.NoSuchElementException: No line foundat java.base/java.util.Scanner.nextLine(Scanner.java:1651)at TMSimulator.main(TMSimulator.java:15)

so what I'm doing is reading in an input string, for the purposes of this example Ill be using 11 from here I use a file handler, read in first id line and then second all it does is put numStates and stopping states into seperate integers so now i have 1 inputs. next, I try to split all the data into lines seperated by the next line delimiter; however i dont know how to handle multiple ,test things in the array and i know eventually using ; isnt whats needed.

i am thinking two things: making an nested array with every combination of [][] into my file path, or doing a method that reads line by line and splits them to only seperate at each empty space in string however... how is it possible to have a case filed variable and a loop together.

2nd idea: my tm has states, so using an indented num can be considered like e and 0 however i would need to preserve some sort of ordering system maybe ascii values?

however when i want to make sure the correct state is being accepted then i will say m = (state1 = 1) < 2; brainf*rk type?

0

There are 0 best solutions below