0

I am trying to code the basic functions of a Turing machine. So far, the program takes user input and stores it into a List as shown below:

    public String cmdLoop()
{

    Scanner getReq = new Scanner( System.in );

    for( ; ; )
    {

        say( "current read/write head position: " + currHeadPos );
        say( "" );

        SecondMenu();
        say( "Type commands here:");

        ArrayList<String>inputs = new ArrayList<String>();
        String userInput = getReq.nextLine();
        do{
            userInput = getReq.nextLine();
            inputs.add(userInput);


                RunCmd(userInput);

        }
        while(!userInput.equals("Done"));
    }
}

SecondMenu is as below

    public void SecondMenu()
{
    say( "\t?\tprint current cell"                );
    say( "\t=\tassign new symbol to current cell" );
    say( "\tE\terase current cell"                );
    say( "\tL\tmove head to left"                 );
    say( "\tR\tmove head to right"                );
    say( "\tB\trewind to beginning of tape"       );
    say( "\tD\tdump contents of tape"             );
    say( "\tT\tset Transition commands"          );
}

public void  say( String  s )
{
    System.out.println( s );
}

Each input is a command. In other words, how the program will run is based on what the user types in, and the method that runs these commands is called RunCmd. Some examples of the commands are below.

    void RunCmd(String userInput)

{       char command = userInput.charAt(0);         
        say("");

        if(command == '?')
        {
            dumpTape(currHeadPos, currHeadPos + 1);
        }
        else
        if(command == '=')
        {

            tapeContents[currHeadPos] = userInput.charAt(1);
            if( endOfTape == 0 )
            {
                endOfTape++;
            }
        }
        else
        if( command == 'E' )
        {
            //use a space to signal 'empty' so that cells will print out ok
            tapeContents[ currHeadPos ] = ' ';
        }
        else
        if( command == 'L' )
        {
            if( currHeadPos == 0 )
            {
                say( "" );
                say( "Tape rewound, unable to move LEFT" );
            }
            else
            {
                currHeadPos--;
            }
        }
        else
        if( command == 'R' )
        {
            currHeadPos++;
            endOfTape++;
        }

How can I get the program to iterate through the loop and run all the input commands at once? For example:

  1. User inputs =1, R, =0, R

  2. The program would make the cell under the header = 1, move Right, = 0, move Right again, all in one run.

*I don't want the program to ask for a command after each input, and typing 'Done' will end inputs. I don't want the program to display SecondMenu anytime other than when the program starts.

*I want to be able to input like 100 inputs once the program runs, store them in a List, and have the program iterate through the array and run all 100 commands (based on user input) in one run. I've tried using for loops, while loops, and iterator (though I have no idea how to use this and may have done it wrong)

Edited for clarification.

Alex Wan
  • 5
  • 3
  • So what's stopping you from adding each input into an array or list, and then looping through the array or list and calling RunCmd() for each one? – D M Feb 08 '17 at 03:09
  • I tried that using a for loop. Maybe I did it wrong, but it didn't work the way I wanted it to. – Alex Wan Feb 08 '17 at 04:21

1 Answers1

0

"Premature optimization is the root of all evil"

Your question confuses me quite a bit, but I hope this can help:

public static void main(String[] args)
{
    Scanner getReq = new Scanner( System.in );

    List<String> commands = new ArrayList<String>();
    String command;
    while (!(command = getReq.nextLine().trim()).equalsIgnoreCase("end"))
    {
        commands.add(command);
    }
    runCmds(commands);
}

private static void runCmds(List<String> userInputs)
{
    for (String userInput : userInputs)
    {
       //your insanity here :)
    }
}
  • To clarify, SecondMenu() is basically a menu that tells you what typing in certain inputs will do. I want the menu to stop showing up after entering each input, and I want to be able to spam all the inputs that I want on the menu in one go (by saving inputs in a list), then have the program look at the array, and run based on all the inputs I typed in also all in one go – Alex Wan Feb 08 '17 at 04:29
  • I don't know if SecondMenu() is backed by a JFrame or some other gui, and I do not understand exactly what an "input" is. Are the inputs command line arguments, like "-n 5", and all on the same line, or all they separate commands on separate lines? – Falcinspire Feb 08 '17 at 04:46
  • Separate commands on separate lines. Some examples of what commands are are in RunCmd. Some are symbols. Mostly letters. – Alex Wan Feb 08 '17 at 14:44