I am working on a project for my Operating Systems class, and we are required to create a program that reads a text file of 3-digit integers, interpret each digit of the integer to complete the specified task, and then print out the value of each register at the end of execution.
I'm certain that my code is not completely eloquent, but the main problem that I have now is that each time I try to run it, I get the error "The system couldn't find a suitable main method." No compiler errors. The name of the Java file is "Assignment1.java" Any help is appreciated.
import java.util.Scanner;
import java.io.*;
public class Assignment1
{
public static void main(String[] args) throws FileNotFoundException
{
try
{
int counter = 0;
int[] registers = new int[10]; // Create an array of integers to represent the instruction registers
for(int i = 0; i < registers.length; i++) // Initialize each register to 0
{
registers[i] = 0;
}
Scanner fileScan = new Scanner(new File("input1.txt"));
fileScan.useDelimiter(" ");
while(fileScan.hasNext())
{
number = fileScan.nextInt(); // Scan the instruction
String instr = number.toString(); // Convert the instruction to a String
if(instr.length()=3 && counter<=1000) // Verify no more than 1000 instructions will be executed and they are 3-digit numbers
{
executeInstr(instr);
}
}
System.out.println("Register 0: " + registers[0]);
System.out.println("Register 1: " + registers[1]);
System.out.println("Register 2: " + registers[2]);
System.out.println("Register 3: " + registers[3]);
System.out.println("Register 4: " + registers[4]);
System.out.println("Register 5: " + registers[5]);
System.out.println("Register 6: " + registers[6]);
System.out.println("Register 7: " + registers[7]);
System.out.println("Register 8: " + registers[8]);
System.out.println("Register 9: " + registers[9]);
System.out.println();
System.out.println("Number of instructions executed: " + counter);
}
catch(FileNotFoundException ex)
{
System.out.println("An error has occured. The file cannot be found.");
}
}
public static void executeInstr(String s) // method to implement instructions
{
instruction = s;
counter++;
int dig1 = (int)instruction.charAt(0); // convert first digit of the instruction to an integer variable
int dig2 = (int)instruction.charAt(1); // convert second digit of the instruction to an integer variable
int dig3 = (int)instruction.charAt(2); // convert third digit of the instruction to an integer variable
case 1: dig1=1
break;
case 2: dig1=2
registers[dig2]=dig3;
break;
case 3: dig1=3
registers[dig2]=registers[dig2]+dig3;
break;
case 4: dig1=4
registers[dig2]=registers[dig2]*dig3;
break;
case 5: dig1=5
registers[dig2]=dig3;
break;
case 6: dig1=6
registers[dig2]=dig2+dig3;
break;
case 7: dig1=7
registers[dig2]=registers[dig2]*dig3;
break;
case 8: dig1=8
break;
case 9: dig1=9
break;
case 10: dig1=0
break;
default:
System.out.println("Invalid entry.");
break;
}
}
One problem I see is that you have nested public static void executeInstr(String s)
within your main method. This is prohibited. Instead, move your method to be nested within your Assignment1
class just like your main method is.
public class Assignment1 {
public static void executeInstr(String s) {
// your method logic here
}
public static void main(String[] args) {
// your main program code here
executeInstr(myString);
}
}
This should compile and run for you.