Hi,

I am developing a TM simulation program. But got stuck in execution as it goes wrong. The Code is the follwing.

1. Java Code

import java.io.IOException;

public class TMSimulation{
	
	public static void main(String args[]) throws IOException{
		
		InputReader reader=new InputReader();
		
		 reader.tmFileNamereader();
		 reader.tmFileReader();
		 
		
		
		
		//fileReader.display(anArray)
	}
}
import java.io.*;

public class InputReader{
	
		private String tmFileName;
		private String inputString;
		
	public InputReader(){
		
		System.out.println("Welcome To The Turing Machine Simulation Program");
		System.out.println("Please Enter The input as described");
		
	}
		
	public String tmFileNamereader() throws IOException{
		
		BufferedReader myReader=new BufferedReader(new InputStreamReader(System.in));
		
		System.out.println("The file name with extension which contains the TM code");
		this.tmFileName=myReader.readLine();
		
		return(this.tmFileName);
		
	}
	
	public String inputStringReader() throws IOException{
		
		BufferedReader myReader=new BufferedReader(new InputStreamReader(System.in));
		
		System.out.println("The input string you want to run on the TM");
		this.inputString=myReader.readLine();
		
		return (this.inputString);
	}
	
	public void tmFileReader() throws IOException{
		
		SimulationWork sw=new SimulationWork();
		FileInputStream fstream = new FileInputStream(this.tmFileName);
		DataInputStream in = new DataInputStream(fstream);
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		
		
		
		String strLine;
		
		
	    //Read File Line By Line
	   while ((strLine = br.readLine()) != null)   {
	      // Print the content on the console
	     // System.out.println (i+"--index--"+strLine);
	      
	     
		   sw.simulateTM(strLine);
		  
	   
	    }
		
	   
	   in.close();
	}
	
}
import java.io.IOException;

public class SimulationWork{
	
	private String inputString;
	private char currentState;
	private char endState;
	private char readSymbol;
	private char writeSymbol;
	private char direction;
	private char prevEndState;
	private int  prevTapeHead;
	private int tapHeadPosition;
	
	//private int i=0;
	
	
	public SimulationWork() throws IOException{
		
		this.inputString=new InputReader().inputStringReader();
		prevEndState='1';
	    prevTapeHead=0;
	    tapHeadPosition=0;
		
	}
	
	public void simulateTM(String tmCodeLine){
		
		currentState=tmCodeLine.charAt(1);
		endState=tmCodeLine.charAt(5);				
		readSymbol=tmCodeLine.charAt(3);
		writeSymbol=tmCodeLine.charAt(7);
		direction=tmCodeLine.charAt(9);
		

		
		if(prevTapeHead==-1){
			
			System.out.println("The Machine is Crashed");
		}
		else if(endState=='2'){
			
			System.out.println("Machine is Halted at state:---"+endState);
		}
		
		
		else if(currentState==prevEndState){
			
				if(readSymbol==inputString.charAt(tapHeadPosition)&& direction=='R'){
					 
					char[]anArray=inputString.toCharArray();
					anArray[tapHeadPosition]=writeSymbol;
					prevTapeHead=tapHeadPosition++;
					prevEndState=endState;
					inputString=String.copyValueOf(anArray);
					
					System.out.println(currentState+"--"+inputString+"--"+tapHeadPosition);
				
				}
				else if(readSymbol==inputString.charAt(tapHeadPosition)&& direction=='L'){
					 
					char[]anArray=inputString.toCharArray();
					anArray[tapHeadPosition]=writeSymbol;
					prevTapeHead=tapHeadPosition--;
					prevEndState=endState;
					inputString=String.copyValueOf(anArray);
					
					System.out.println(currentState+"--"+inputString+"--"+tapHeadPosition);
					
				}
				else{
					
					System.out.println("Your Input was incorrect");
				}
				
		
	}
	
	}
}

2.. Sample file for TM Code (Increment TM)

(1,$,3,$,R)
(3,0,3,0,R)
(3,1,3,1,R)
(3,#,4,#,L)
(4,1,4,0,L)
(4,0,2,1,R)

Thanks very much for help in advance

What do you expect to happen as a result of the input you showed above? What is going wrong? Where in your code are you getting an error - and is it a logical error, or is the code throwing an exception? And what are you trying to do that you are having trouble with? Don't assume that everyone here has intimate knowledge of the TM problem that you have; we aren't in your class. And you need to provide a lot more information to get help.

commented: Many fine points. +20
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.