I have this project that I am doing for a college course, creating a OS simulator. The program is supposed to read from an input file, place the instructions one at a time into 'memory' (an array), and then use the instructions to get, manipulate, and print out the data from the input file to an output file. Here is my code and the errors that come up. (I realize that it's a tiny bit sloppy, but nothing that I think should be affecting the compiling or running, or giving me these errors). Any ideas what it could be? Any help ASAP would
greatly be appreciate. Thank you.
import java.io.*;
public class MasterModerevised{
public String[] M;
public String[] IR;
public int IC;
public String[] R;
public boolean C;
public int SI;
File f;
FileInputStream fis;
BufferedInputStream bis;
DataInputStream dis;
FileWriter out;
PrintWriter p;
public static void main(String [] args) throws java.io.IOException{
MasterModerevised mine = new MasterModerevised();
mine.MasterMode();
}
public MasterModerevised() throws java.io.IOException{
M = new String[100];
IR = new String[2];
R = new String[2];
SI = 3;
f = new File("C:/Input.txt");
fis = new FileInputStream(f);
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
out = new FileWriter("Output.txt", true);
p = new PrintWriter(out);
}
public void MasterMode() throws java.io.IOException{
switch (SI){
case 1: Read();
break;
case 2: Write();
break;
case 3: Terminate();
break;
}
}
public void Read() throws java.io.IOException{
String line = new String();
IR[1] = IR[1].charAt(0)+"0";
line= dis.readLine();
int index= 0;
int x=0;
int spacevalue=40-line.length();
while(spacevalue!=0)
{
line+=" ";
spacevalue--;
}
while(index<=line.length()-4)
{
M[Integer.parseInt(IR[1])+x]=line.substring(index,4);
x++;
index+=4;
}
ExecuteUserProgram();
}
public void Write() throws java.io.IOException{
IR[1] = IR[1].charAt(0)+"0";
for(int i=0;i<10;i++)
{
p.print(M[Integer.parseInt(IR[1])+i]);
p.flush();
}
p.println("/n");
p.flush();
ExecuteUserProgram();
}
public void Terminate() throws java.io.IOException{
//write two blank lines
p.print("/n/n");
Load();
}
public void Load() throws java.io.IOException{
int index = 0;
int x = 0;
for(int i = 0; i<100; i++){
M[i] =null;
}
int memi=0; //each new amj resets memory,(no multiprogramming)
String line = new String();
while((line = dis.readLine()) != null)
{
line=dis.readLine();
if(line.substring(0,4).compareToIgnoreCase("$AMJ")==0)
{
// outfile line.substring(4,4);
}
else if (line.substring(0,4).compareToIgnoreCase("$DTA")==0)
{
StartExecution();
}
else if (line.substring(0,4).compareToIgnoreCase("$END")==0){
}
else
{
if(x >= 100)
{
System.err.println("Memory overflow input less instructions");
// System.exit();
}
line= dis.readLine();
int spacevalue=40-line.length(); //We dont need spacevalues for instruction line
if(spacevalue<0)
{
System.err.println("Too many instructions per line");
// System.exit();
}
// while(spacevalue!=0)
// {
// line+=" ";
// spacevalue--;
// }
while(index<=line.length())
{
if(String.valueOf(line.charAt(index)).equals("H")){
String h = String.valueOf(line.charAt(index));
M[x] = h;
}
else{
M[x]=line.substring(index,4);
x++;
index+=4;
}
}
}
}
}
public void StartExecution() throws java.io.IOException{
IC = 0;
ExecuteUserProgram();
}
public void ExecuteUserProgram() throws java.io.IOException{
IR[0] = M[IC].substring(0,1);
IR[1] = M[IC].substring(2,3);
IC = IC + 1;
if (IR[0].compareToIgnoreCase("LR") == 0){
R[1] = M[Integer.parseInt(IR[1])];
}
else if (IR[0].compareToIgnoreCase("SR") == 0){
M[Integer.parseInt(IR[1])] = R[1];
}
else if (IR[0].compareToIgnoreCase("CR") == 0)
{
if (R[Integer.parseInt(IR[1])] == M[Integer.parseInt(IR[1])])
C = true;
else
C = false;
}
else if (IR[0].compareToIgnoreCase("BT") == 0)
{
if (C == true)
IC = Integer.parseInt(IR[1]);
}
else if (IR[0].compareToIgnoreCase("GD") == 0)
{
SI = 1;
MasterMode();
}
else if (IR[0].compareToIgnoreCase("PD") == 0)
{
SI = 2;
}
else if (IR[0].compareToIgnoreCase("H") == 0)
{
SI = 3;
}
}
}
and when i try to run.....
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind
ex out of range: -4
at java.lang.String.substring(String.java:1444)
at MasterModerevised.Load(MasterModerevised.java:148)
at MasterModerevised.Terminate(MasterModerevised.java:94)
at MasterModerevised.MasterMode(MasterModerevised.java:49)
at MasterModerevised.main(MasterModerevised.java:22)
Press any key to continue...