Results 1 to 5 of 5
  1. #1
    connorlm3 is offline Member
    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Default No Main Method Found?

    I missed class two days last week. My class did a program together and I asked the teacher what I need to do. He said contents of it could be on the test so I need to get it from another student and see how it works. I asked a student to email me it, when he did i copied and pasted it into netbeans, went to run it, and keep getting a no main method found error? The main method has a static before it, so it isnt because of that. So what is the problem?

    Java Code:
    package mat2670;
    
    import java.util.*;
    
    public class MagicSquare {
        // The n/sidelength value of the magicsquare 
    
        private static int magicNumber;
        // Array of integers holding the user's inputs   
        private static int[] inputs;
        // Double array containing the magic square 
        private static int[][] magicSquare;
        private static int count = 0;
    
        public static void main(int args) {
            
            magicNumber = args;
            magicSquare = new int[args][args];
            inputs = new int[(int) Math.pow(Double.valueOf(args), 2)];
        
        }
    
        /**
         *
         * @return the magic constant m using the formula m = n * (n^2 + 1) / 2 ex.
         * n = 3 m = 3 (3^2 +1) 2 m = 15
         */
        private static int computeMagicConstant() {
            
            return (int) (magicNumber * (Math.pow(magicNumber, 2) + 1) / 2);
    
        }
    
        /**
         *
         * @return true if the sum of rows equals magic constant
         */
        private static boolean checkRows() {
            for (int row = 0; row < magicSquare.length; row++) {
                int tempSum = 0;
                for (int col = 0; col < magicSquare.length; col++) {
                    tempSum += magicSquare[row][col];
                }
                if (tempSum != computeMagicConstant()) {
                    return false;
                }
            }
    
            return true;
        }
    
        /**
         *
         * @return true if the sum of columns equals magic constant
         */
        private static boolean checkColumns() {
            for (int col = 0; col < magicSquare.length; col++) {
                int tempSum = 0;
                for (int row = 0; row < magicSquare.length; row++) {
                    tempSum += magicSquare[row][col];
                }
                if (tempSum != computeMagicConstant()) {
                    return false;
                }
            }
    
            return true;
        }
    
        /**
         *
         * @return true if sum of diagonals equals magic constant
         */
        private boolean checkDiagonals() {
    
            // Check bottom left to top right
            for (int cornerRow = getMagicSquare().length - 1; cornerRow >= 0; cornerRow--) {
                int tempSum = 0;
                for (int cornerColumn = 0; cornerColumn < getMagicSquare().length; cornerColumn++) {
                    tempSum += magicSquare[cornerRow][cornerColumn];
                }
                if (tempSum != computeMagicConstant()) {
                    return false;
                }
    
            }
    
            // Check bottom right to top left
            for (int cornerRow = getMagicSquare().length - 1; cornerRow >= 0; cornerRow--) {
                int tempSum = 0;
                for (int cornerColumn = getMagicSquare().length - 1; cornerColumn >= 0; cornerColumn--) {
                    tempSum += magicSquare[cornerRow][cornerColumn];
                }
                if (tempSum != computeMagicConstant()) {
                    return false;
                }
            }
    
            return true;
        }
    
        /**
         * Checks if userinput is the full range of numbers from n to n^2 Checks if
         * number of inputs equals n^2
         *
         * @return
         */
        public static boolean checkInput() {
            // Convert the array to a Set of integers. Duplicates will be eliminated   
            Set<Integer> setMagicSquare = new HashSet<Integer>();
            for (int i = 0; i < inputs.length; i++) {
                setMagicSquare.add(inputs[i]);
            }
    
            // Check if the number of values equals n^2
            if (setMagicSquare.size() != Math.pow(magicNumber, 2)) {
                return false;
            }
    
    
            // Check if all values from 1 to n^2 is present
            for (int i = 1; i <= Math.pow(magicNumber, 2); i++) {
                if (!setMagicSquare.contains(i)) {
                    return false;
                }
            }
            return true;
        }
    
        /**
         *
         * @param i a value typed in by the user
         */
        public static void addInt(int i) {
            inputs[count] = i;
            count++;
        }
    
        /**
         *
         * @return true if the numbers form a magicsquare
         */
        public boolean isMagic() {
            fillMagicSquare();
    
            return checkInput() && checkRows()
                    && checkColumns()
                    && checkDiagonals();
        }
    
        /**
         *
         * @return an instance of the magicsquare
         */
        public int[][] getMagicSquare() {
            return this.magicSquare;
    
        }
    
        /**
         * Creates a magicsquare array
         */
        private static void fillMagicSquare() {
            int counter = 0;
            for (int row = 0; row < magicNumber; row++) {
                for (int col = 0; col < magicNumber; col++) {
                    magicSquare[row][col] = inputs[counter];
                    counter++;
                }
            }
    
        }
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default Re: No Main Method Found?

    Please post the full text of the error message. It has important information.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: No Main Method Found?

    Your signature for the main method is wrong. It must be
    Java Code:
    public static void main(String [] args) {
    ...
    ...
    
    }
    Any numeric values passed to main must be converted to numeric types.

    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    connorlm3 is offline Member
    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Default Re: No Main Method Found?

    Ooh ok, can i change args into an int with Integer.ParseInt?

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: No Main Method Found?

    Yes.

    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Main method not found in class
    By kask382 in forum New To Java
    Replies: 8
    Last Post: 02-14-2013, 05:28 PM
  2. Replies: 8
    Last Post: 11-22-2012, 08:16 AM
  3. main method not found error in class . wat s the error?
    By kirankumaragnihotram in forum New To Java
    Replies: 10
    Last Post: 06-29-2012, 12:26 PM
  4. Main Method not found in class
    By HinaKhan in forum New To Java
    Replies: 6
    Last Post: 06-22-2012, 03:34 PM
  5. main method not found in class
    By eLancaster in forum New To Java
    Replies: 5
    Last Post: 02-09-2011, 09:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •