0

I have declared a class main where I am calling the main function. I have commented the main function in Class Stack as I am calling the push and pop functions in the class Main. I have put up the code and the error I am facing.

CODE:

public class Stack {

    //public static void main(String[] args) {
        // TODO Auto-generated method stub
        static final int max = 10;
        int top = -1;
        int a[]= new int[max];

        static void push(int data)
        {   
            if (top>=max -1)
                    System.out.println("OVerflow");
            else
                    a[++top]= data;
        }
        static int pop()
        {   
            if (top<0)
                    System.out.println("Empty Stack");
            else
                    {int x = a[--top];
                    return x;
        }

        }
}
        //}
class main {
    public static void main(String[] args) {
        Stack s = new Stack();
        s.push(10);
        s.push(20);
        s.pop();
        s.push(9);
        s.pop();
        for (i =0; i< 10;i++)
        { 
            System.out.println(a[i]);
        }               
    }
}

ERROR:

Error: Main method not found in class Stack, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
Drool
  • 119
  • 1
  • 7
  • But you're still trying to execute Stack instead of main (which should be named Main to respect the Java naming conventions). – JB Nizet Dec 24 '18 at 21:09
  • And your code doesn't compile anyway, so you shouldn't even be trying to execute anything. Read and fix the cmpilation errors first. – JB Nizet Dec 24 '18 at 21:11

5 Answers5

0

The reason why the error is happening is that you need to tell the compiler the where the main method is. You need to go find the run configurations option in your editor, and from there make it run Main and not Stack. I am using Eclipse IDE.

Eclipse: https://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftasks-java-local-configuration.htm

Intellij: https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000156824-How-to-choose-main-class

The fixed code, FINAL:

package foo;

public class Stack {

    private final int max = 10;
    private int top = -1;
    private int a[] = new int[max];

    public int[] getA() {
         return a;
    }

    public void push(int data) {
        if (top >= max - 1)
            System.out.println("OVerflow");
        else
            a[++top] = data;
        }

    public int pop() {
        if (top < 0)
            System.out.println("Empty Stack");
        else {
            int x = a[--top];
            return x;
        }
        return top;
    }
}

class Main {
    public static void main(String[] args) {
        Stack s = new Stack();
        s.push(10);
        s.push(20);
        s.pop();
        s.push(9);
        s.pop();

        int[] a = s.getA();

        for (int i = 0; i < 10; i++) {
            System.out.println(a[i]);
        }
    }
}
Compiler v2
  • 3,509
  • 10
  • 31
  • 55
0

your code works after changing class access specifier and removing few bug. (I am using eclipse)

class Stack {

    static final int max = 10;
    static int top = -1;
    static int a[] = new int[max];

    static void push(int data) {
        if (top >= max - 1)
            System.out.println("OVerflow");
        else
            a[++top] = data;
    }

    static int pop() {
        int x = 0;
        if (top < 0)
            System.out.println("Empty Stack");
        else {
            x = a[--top];
            return x;
        }
        return x;

    }
}

class main {
    public static void main(String[] args) {
        Stack s = new Stack();
        s.push(10);
        s.push(20);
        s.pop();
        s.push(9);
        s.pop();
        for (int i = 0; i < 10; i++) {
            System.out.println(s.a[i]);
        }
    }
}
5udip
  • 16
  • 1
0

This is another possible solution, including main method in your public class 'Stack'. Also, you are using static methods, you need global static variables.

public class Stack {
// public static void main(String[] args) {
// TODO Auto-generated method stub
static final int max = 10;
static int top = -1;
static int a[] = new int[max];

static void push(int data) {
    if (top >= max - 1)
        System.out.println("OVerflow");
    else
        a[++top] = data;
}

static int pop() {
    if (top < 0)
        System.out.println("Empty Stack");
    else {
        int x = a[--top];
        return x;
    }
    return 0;

}

public static void main(String[] args) {
    // Stack s = new Stack();
    push(10);
    push(20);
    pop();
    push(9);
    pop();
    for (int i = 0; i < 10; i++) {
        System.out.println(a[i]);
    }
}
}

Output: 10 9 0 0 0 0 0 0 0 0

0

You have a few problems that needs to be fixed first so that your code compiles.

You are using a object of Stack class by Stack s = new Stack(); so you don't need your push() and pop() method to be static so get rid of static from method declaration. It will look like

void push(int data)
{
    if (top>=max -1)
        System.out.println("OVerflow");
    else
        a[++top]= data;
}

Another problem is your pop() method. You have given a return type int. therefore it must always return an int. Your methods last line should contain a return value which is int. So you should change your pop() value to something like

int pop() {
    int poppedValue = 0;
    if (top < 0)
        System.out.println("Empty Stack");
    else {
        poppedValue = a[--top];
    }

    return poppedValue;
}

Your main class do not have access to the Stack class's int[] a however you attempt to iterate through it. You can add a method in your Stack class to return you the int[] a array.

public int[] getStack() {
    return a;
}

So after the push() and pop() in your main() method you can retrieve the array by.

int [] a = s.getStack();

Your last problem is in the for loop. you have to specify the data type of i. so your loop should look like.

    int [] a = s.getStack();
    for (int i =0; i< 10;i++)
    {
        System.out.println(a[i]);
    }

Also notice since you are getting the a array from Stack object s your loop body will be able to access a[i] previously this would have been a compilation error.

rumman0786
  • 1,150
  • 10
  • 20
-1

You need to place the main method inside the public class. So either make class main public and class Stack default access or move the main method inside the Stack class and dispose of the main class.

First solution:

class Stack{
   //...
}

public class Main{

    public static void main(String[] args){

    }
}

Second solution:

public class Stack {
    //...
    public static void main(String[] args){

    }
}

See also: What if main method is inside "non public class" of java file?

N Sarj
  • 374
  • 3
  • 11