-1

I coded an program to assemble some text into binary code. The compiler always gives an error in the main method and tells me that it is not static...can someone help me please???? I tried to make it static but the error is there anyway. I hope someone can help me.

public class Main {
     private String file;
     private int pc = 0;

    public  Main( String fin ) {
        file = fin;
    }

    public void main( String[] args ) throws IOException, Exception {
        // CREATE INSTANCES OF OTHER MODULES
        Parser fp = new Parser( args[ 0 ] );
        Parser sp = new Parser( args[ 0 ] );
        Code code = new Code();
        HashMap<String, String> st = new HashMap<String, String>();

        // SYMBOL TABLE INITIALIZATION
        st.put( "R0", "0" ); st.put( "R1", "1" ); st.put( "R2", "2" ); st.put( "R3", "3" ); st.put( "R4", "4" ); st.put( "R5", "5" ); st.put( "R6", "6" ); st.put( "R7", "7" );
        st.put( "R8", "8" ); st.put( "R9", "9" ); st.put( "R10", "10" ); st.put( "R11", "11" ); st.put( "R12", "12" ); st.put( "R13", "13" ); st.put( "R14", "14" ); st.put( "R15", "15" );
        st.put( "SCREEN", "16384" ); st.put( "KBD", "24576" );
        st.put( "SP", "0" ); st.put( "LCL", "1" ); st.put( "ARG", "2" ); st.put( "THIS", "3" ); st.put( "THAT", "4" );

        // FIRST PASS
        fp.advance();
        while( fp.command != null ) {
            if( fp.commandType() == "L_COMMAND" ) {
                st.put( fp.symbol(), Integer.toString( pc ) );
                pc--;
            }
            fp.advance();
            pc++;
        }

        // SECOND PASS
        FileWriter writer = null;
        int rAllocation = 16; // Keeps a record of the last register allocated to a variable.

        try {
            // CREATE FILE, FILE WRITER
            File nf = new File( file.replaceAll( "\\.asm", ".hack" ) );
            nf.createNewFile();
            writer = new FileWriter( nf );

            // SECOND PASS
            sp.advance();
            while( sp.command != null ) {
                if( sp.commandType() == "L_COMMAND" ) {
                    // Do nothing.
                } else if( sp.commandType() == "A_COMMAND" ) {
                    if( !( Pattern.compile( "[a-zA-Z]" ).matcher( sp.symbol() ).find() ) ) { // If the symbol consists of only digits.
                        writer.write( convertAddr( sp.symbol() ) + "\n" ); // Translate integer value to binary, write to file.
                    } else if( st.get( sp.symbol() ) == null ){
                        st.put( sp.symbol(), Integer.toString( rAllocation ) ); // Assign the variable an unoccupied register.
                        rAllocation++;
                        writer.write( convertAddr( st.get( sp.symbol() ) ) + "\n" );  // Retrieve the just allocated value from SymbolTable, translate to binary, write.
                    } else {
                        writer.write( convertAddr( st.get( sp.symbol() ) ) + "\n" );  // Retrieve value of symbol from SymbolTable, translate to binary, write.
                    }
                } else if( sp.commandType() == "C_COMMAND" ) {
                    String d = code.dest( sp.dest() );
                    String c = code.comp( sp.comp() );
                    String j = code.jump( sp.jump() );

                    writer.write( "111" + c + d + j + "\n" );
                }
                sp.advance();
            }
        } catch( IOException e ) {
            e.printStackTrace();
        } finally {
            // CLOSE WRITER
            writer.flush();
            writer.close();
        }
    }

    private String convertAddr( String addr ) throws Exception{
        String bin;
        String zeroPad = "";
        if( addr != null ) {
            bin = Integer.toBinaryString( Integer.parseInt( addr ) );
            for( int i = 0; i < ( 16 - bin.length() ); i++ ) {
                zeroPad += "0";
            }
            return zeroPad + bin;
        } else {
            throw new Exception( "Null Parameter." );
        }
    }
}


there are two other classes, parser and code, that are related to the main class

basti
  • 41
  • 3

3 Answers3

2

Your main method needs to be

public static void main(String[] args){}

The static is needed because non-static methods are called on objects of a class - and at the start of your programm no objects have been created yet. Static methods, however, are called on classes, meaning that they can be called as soon as the class is loaded, but can only access other static resources of the class.

This means your pc and file variables either need to be static as well, or you need to move your code to different method and create a Main object in your main method and call that new method on your new object, like this:

public class Main(){
    private String file;
    private int pc = 0;

    public Main(String fin){
        file=fin;
    }

    public static void main(String[] args){
        Main main = new Main(args[0]); //or wherever the filename is in args
        main.doStuff();
    }

    public void doStuff(){
        //your code
    }
}
Reyem
  • 31
  • 6
1

Make variables and Main method as static like below. It should work

public class Main {
    private static String file;
    private static int pc = 0;

   public  Main( String fin ) {
       file = fin;
   }

   public static void main( String[] args ) throws  Exception {
       // CREATE INSTANCES OF OTHER MODULES
       Parser fp = new Parser( args[ 0 ] );
       Parser sp = new Parser( args[ 0 ] );
       Code code = new Code();
       HashMap<String, String> st = new HashMap<String, String>();

       // SYMBOL TABLE INITIALIZATION
       st.put( "R0", "0" ); st.put( "R1", "1" ); st.put( "R2", "2" ); st.put( "R3", "3" ); st.put( "R4", "4" ); st.put( "R5", "5" ); st.put( "R6", "6" ); st.put( "R7", "7" );
       st.put( "R8", "8" ); st.put( "R9", "9" ); st.put( "R10", "10" ); st.put( "R11", "11" ); st.put( "R12", "12" ); st.put( "R13", "13" ); st.put( "R14", "14" ); st.put( "R15", "15" );
       st.put( "SCREEN", "16384" ); st.put( "KBD", "24576" );
       st.put( "SP", "0" ); st.put( "LCL", "1" ); st.put( "ARG", "2" ); st.put( "THIS", "3" ); st.put( "THAT", "4" );

       // FIRST PASS
       fp.advance();
       while( fp.command != null ) {
           if( fp.commandType() == "L_COMMAND" ) {
               st.put( fp.symbol(), Integer.toString( pc ) );
               pc--;
           }
           fp.advance();
           pc++;
       }

       // SECOND PASS
       FileWriter writer = null;
       int rAllocation = 16; // Keeps a record of the last register allocated to a variable.

       try {
           // CREATE FILE, FILE WRITER
           File nf = new File( file.replaceAll( "\\.asm", ".hack" ) );
           nf.createNewFile();
           writer = new FileWriter( nf );

           // SECOND PASS
           sp.advance();
           while( sp.command != null ) {
               if( sp.commandType() == "L_COMMAND" ) {
                   // Do nothing.
               } else if( sp.commandType() == "A_COMMAND" ) {
                   if( !( Pattern.compile( "[a-zA-Z]" ).matcher( sp.symbol() ).find() ) ) { // If the symbol consists of only digits.
                       writer.write( convertAddr( sp.symbol() ) + "\n" ); // Translate integer value to binary, write to file.
                   } else if( st.get( sp.symbol() ) == null ){
                       st.put( sp.symbol(), Integer.toString( rAllocation ) ); // Assign the variable an unoccupied register.
                       rAllocation++;
                       writer.write( convertAddr( st.get( sp.symbol() ) ) + "\n" );  // Retrieve the just allocated value from SymbolTable, translate to binary, write.
                   } else {
                       writer.write( convertAddr( st.get( sp.symbol() ) ) + "\n" );  // Retrieve value of symbol from SymbolTable, translate to binary, write.
                   }
               } else if( sp.commandType() == "C_COMMAND" ) {
                   String d = code.dest( sp.dest() );
                   String c = code.comp( sp.comp() );
                   String j = code.jump( sp.jump() );

                   writer.write( "111" + c + d + j + "\n" );
               }
               sp.advance();
           }
       } catch( IOException e ) {
           e.printStackTrace();
       } finally {
           // CLOSE WRITER
           writer.flush();
           writer.close();
       }
   }

   private String convertAddr( String addr ) throws Exception{
       String bin;
       String zeroPad = "";
       if( addr != null ) {
           bin = Integer.toBinaryString( Integer.parseInt( addr ) );
           for( int i = 0; i < ( 16 - bin.length() ); i++ ) {
               zeroPad += "0";
           }
           return zeroPad + bin;
       } else {
           throw new Exception( "Null Parameter." );
       }
   }
}
0

The main method has to be static so that the main method can be called without creating an instance of the main class. Just change to static.