-3

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;
            }
}
Morgan4568771
  • 153
  • 2
  • 4
  • 14
  • 1
    How, exactly, did you try to run it? It might also help to know what OS you're using. – ajb Feb 09 '15 at 00:48
  • Are you sure the code you've shown us is the code you've compiled? The code you've posted appears to have a method (`executeInstr`) nested directly inside your main method, which isn't allowed in Java. So it shouldn't have compiled, with the code you've given us. – ajb Feb 09 '15 at 00:52
  • Windows 8.1. Yes, the code in its entirety is what I compiled. I'm using JCreator Pro if that makes a difference. – Morgan4568771 Feb 09 '15 at 01:10
  • @John3136, if you would have bothered to read the entire block, you would have saw that it was indeed there. No need to be rude when someone is asking for help. – Morgan4568771 Feb 09 '15 at 01:11
  • A correct Java compiler (with the nested `executeInstr`) will not compile this code--period. If you think it compiled without errors, you have apparently missed something--such as, maybe, the compiler is actually compiling a different `Assignment1.java` that you have lying around in a different folder/directory. Which could explain the "cannot run" problem, as well. You may want to edit your file and put a really blatant error in it to make sure the compiler is actually compiling the file you think it's compiling. – ajb Feb 09 '15 at 02:07

1 Answers1

1

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.

Brian
  • 7,098
  • 15
  • 56
  • 73
  • I've tried your suggestion, but I still get the same error, though I do understand that this was part of the problem. I am editing my code in the original question to reflect this. Thanks for your help. – Morgan4568771 Feb 09 '15 at 01:08
  • @Morgan It could also be that you are not running the correct file. This could happen if you choose to run the wrong file in your IDE or make a filename typo using the java command. Make sure you run Assignment1 – Brian Feb 10 '15 at 15:15
  • It turns out that I had not properly configured the JDK. After some minor editing, everything is running properly now. Thanks for your help! That's what led me to the solution. – Morgan4568771 Feb 13 '15 at 02:29
  • @Morgan4568771 Glad it helped. Can you please uproot and accept the answer? – Brian Feb 13 '15 at 06:04