0

i found problems here it says,,

non-static method blah blah blah cannot be referenced from a static context

which is the function convertToPostfix and convertToInfix is not a static method....

how to fix this, so that i can now compile my code.

   public static void main (String[] args) 
    {
        String n, result;
        char character;
        do {
        character = choiceko();
        switch (character)
        {
            case 'P':
            case 'p': n=numberko("Enter a number: ");
              =======>  result = convertToPostfix(n); <============
                JOptionPane.showMessageDialog(null,
               "Infix: " + n + " to Postfix [" +result+"].");
               break;
            case 'I':       
            case 'i': n=numberko("Enter a Number: ");
              =======>  result=convertToInfix(n);<============
                JOptionPane.showMessageDialog(null,
                "Postfix: " + n + " to Infix [" +result+"].");
                break;
            case 'E':
            case 'e':  JOptionPane.showMessageDialog(null, 
                "Program Terminated...","Terminated",JOptionPane.WARNING_MESSAGE);
                break;              
            default:  JOptionPane.showMessageDialog(null,
                " Invalid selection. Please Try Again.","ERROR",JOptionPane.WARNING_MESSAGE);           
    }
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
jarnthrax
  • 146
  • 3
  • 4
  • 11
  • How is this related to Swing? Yes you do have a JOptionPanel but the question itself has no relevance to Swing. Please tag your question more appropriately next time. – Paul Samsotha Dec 28 '13 at 08:01

2 Answers2

0
 =======>  result=convertToInfix(n);<============

since you are accessing convertToInfix method in a static context(main method which is static), that method should be a static method.

Solutions:

1) Make convertToInfix method static

or

2) Make an instance of class which have the method convertToInfix

case 'i': n=numberko("Enter a Number: ");
              ClassOfMethod instance = new ClassOfMethod(); 
              result=instance.convertToInfix(n);
              JOptionPane.showMessageDialog(null,
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

As these 2 methods convertToPostfix and convertToInfix are non static so you need to create an object of the class and then call these methods.It is because main is static.Lets say your class name is test.Then do like this

test t=new test();
t.convertToPostfix(n);
t.convertToInfix(n);
SpringLearner
  • 13,738
  • 20
  • 78
  • 116