1

I've recently started using java and I am trying to make a program that checks to see if an array can be changed to ascending order by returning true/false.

That is the gist of it however I am having problems with the main class. The error I am getting is that the main method is not found in class.

public static boolean solution (int[] A){
 int count = 0;
.......
.......
.......
for(int i=0; i<A.length; i++)
    {
        if(A[i] != B[i]) count++;
    }
    if(count > 2) return false;
    return true;
  }
  }

Since I am doing this as my java summer homework I am abit confused as to where I should add the main method. I know it is supposed to be

public static void main (String args[])

However if I were to add that from the start of the code after the class I get errors. Is it because I cannot have

public static boolean and public static void main 

in the same class?

Thanks.

Mickael
  • 3,506
  • 1
  • 21
  • 33
  • 1
    No, it is not because you cannot have `public static boolean` and `public static void main` in the same class. Just add the `main` method to the class, next to the `solution` method. – Jesper Jul 26 '17 at 12:45
  • All programs must have a main. The main should be inside the class. "Where" exactly inside the class doesn't matter as long as it isn't inside of anything else. – Carcigenicate Jul 26 '17 at 12:46
  • `I am trying to make a program that checks to see if an array can be changed to ascending order by returning true/false` - well here is your solution : `public boolean canBeSorted(final int[] arr) { return arr != null; }`. Everything valid **can** be sorted, even empty arrays. – specializt Jul 26 '17 at 12:50

4 Answers4

0

You can have public static boolean and public static void main in the same class. A main method can be added anywhere within a class as long as you define it as its own standalone method, don't declare inside another method. For Example:

    public class Example{

        public void method1(){
        ....
        }
         public void method2(){
        ....
        }
        public static void main(String[] args){
        ....
        }
}
ja08prat
  • 154
  • 10
0

oke if you are using an ide like intellij you must also configure which class has the main method in you project.

tobash
  • 9
  • 1
0

You CAN have a main method and other static methods in the same class. In fact you can have any type of method in the class. The reason your program cannot "find a main method" is because there either isn't one there, or your run configuration is off.

A java program starts, with some exceptions of the initializer blocks, at the main method. So you MUST have it in at least one class in your program.

From what it sounds like you want to test if the array is already in ascending order. Since all arrays that are not empty, can be sorted.

public class Example{    

    public static void main(String[] args){
        int[] test = {1,2,3,4,5};
        System.out.println(solution(test));
    }

    public static boolean solution (int[] a){
        //returns true if in a
        for (int i = 0; i < a.length-2; i++) {
            if (a[i] > a[i + 1]) 
                return false;
        }
        return true;
    }
}

If your cannot find main method error continues. It is probably your run configuration

Ryan
  • 1,972
  • 2
  • 23
  • 36
0

If you are getting an error, based on the way you phrased your question, it sounds like you have a method with the exact same method signature. You can not have:

public static void main(String[] args)
{

}

public static boolean main(String[] args)
{
  return false;
}

If this is what you have, you should probably rename the second method.

ChrisThompson
  • 1,998
  • 12
  • 17