Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
hi guys
i coded this program
but there is an error in line 127 that is saying:
non-static variable cannot be referenced from a static
what should i do ?!!:((

/////
Java
package itp2;
import java.util.*;
import java.io.*;
class StackX
{
private int maxSize;
private char[] stackArray;
private int[] stackArray1;
private int top;
 
public StackX(int s)
{
maxSize = s;
stackArray = new char[maxSize];
top = -1;
}
 
public void push(char j)
{ stackArray[++top] = j; }
 
public void push1(int j)
{ stackArray1[++top] =j ; }
 
public char pop()
{ return stackArray[top--]; }
public int pop1()
{ return stackArray[top--]; }
public char peek()
{ return stackArray[top]; }
 
public boolean isEmpty()
{ return (top == -1); }
 
public int size()
{ return top+1; }
 
public char peekN(int n)
{ return stackArray[n]; }
 
 
 
} // end class StackX used from lab1
 
class InToPost // infix to postfix conversion borrowed from book
{
private StackX theStack;
private String input;
private String output = "";
 
public InToPost(String in) // constructor
{
input = in;
int stackSize = input.length();
theStack = new StackX(stackSize);
}
 
public String doTrans() // algorithm that does translation with cases
{
for(int j=0; j<input.length(); j++)
{
char ch = input.charAt(j);
 
switch(ch)
{case '+':gotOper(ch, 1); break;
case '-':gotOper(ch, 1); break;
case '*':gotOper(ch, 2); break;
case '/':gotOper(ch, 2); break;
default :output = output + ch; break; //writes to output
} // end of switch
} // end for loop
while( !theStack.isEmpty() ) // pop remaining operators at the end
{
output = output + theStack.pop(); // write to output
}
return output; // return postfix
}
//--------------------------------------------------------------
public void gotOper(char opThis, int prec1)
{
while( !theStack.isEmpty() )
{
char opTop = theStack.pop();
 
if(1==1 )
{
int prec2;
 
if(opTop=='+' || opTop=='-') // find new op prec
prec2 = 1;
else
prec2 = 2;
if(prec2 < prec1)
{
theStack.push(opTop);
break;
}
else
output = output + opTop;
}
} // end while loop
theStack.push(opThis);
}
}


 
 

public class ITP2 {
 
public static void main(String[] args) throws IOException
{
String output;
String input;
while(true)
{
System.out.print("Enter infix: ");
System.out.flush();
input = getString();
if( input.equals("") )
break;
 
InToPost theTrans = new InToPost(input);
output = theTrans.doTrans();
//Eval x=new Eval();
 System.out.println("Evaluated expression: " + (output));
 System.out.println("Postfix is " + evaluate(output)) ;//// line 127
}
}
 
   class Eval{
private StackX operatorStack ;
private StackX operandStack;
private String output;
Eval (){
    
}
public int evaluate( String output)
{
StringTokenizer s = new StringTokenizer(output);//divides into tokens
 
int value;
String symbol;
while (s.hasMoreTokens())
{
symbol = s.nextToken();
if (Character.isDigit(symbol.charAt(0)))// if its a number push it
 
{
Integer operand = new Integer(Integer.parseInt(symbol));
operandStack.push1(operand);
}
else // if it's an operator, operate on the previous two popped operandStack items
{
int op2 = ((Integer)operandStack.pop1()).intValue();
int op1 = ((Integer)operandStack.pop1()).intValue();
int result = 0;
switch(symbol.charAt(0))
{
case '*': {result = op1 * op2; break;}
case '+': {result = op1 + op2; break;}
case '-': {result = op1 - op2; break;}
case '/': {result = op1 / op2; break;}
case '%': {result = op1 % op2; break;}
}
Integer operand = new Integer(result);
operandStack.push1(operand);
}
}
value = ((Integer)operandStack.pop1()).intValue();
return value;
}
 
}



 public static String getString() throws IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
return s;
}

    
}
Posted
Updated 24-Oct-11 1:40am
v3
Comments
OriginalGriff 24-Oct-11 7:37am    
So, do you want to show us which line is line 127, or are we supposed to count them all?
DominoBoy 24-Oct-11 7:41am    
added ///line 127
Arndt Faulhaber 24-Oct-11 7:39am    
Can you highlight (e.g. by Comment in Code) where the problem occurs, otherwise it is rather time-consuming to give a good answer. Generally there are multiple solutions: 1) make the non-static variable static (if possible and it makes sense), 2) create an object containing the variable, and access it via the object, 3) make the context from which you want to access the non-static variable also non-static (if possible)...
Cheers, Arndt
DominoBoy 24-Oct-11 7:40am    
i added ///// line 127
Philippe Mori 24-Oct-11 8:19am    
Indent your code correctly if you want someone to read it!

The problem occurs when you call the non-static function evaluate from the static function main. All you should have to do is make your evaluate function static as well. public static int evaluate( String output)

The difference between static and non-static is that you can use the static functions without having to make an instance of the class. With the BufferedReader for example before you can use the readLine function you need to make a new instance of the class, whereas with a static function you can just call it:
StackX.evaluate(...) instead of having to do StackX myStack = new StackX(); myStack.evaluate(...)



EDIT: Sorry I thought the evaluate function was in the same class as the main function. Another excellent reason for making sure you code is correctly formatted.

Like Philippe Mori mentioned, on line 125 you've commented out the code I suspect you might need. You should uncomment that and use x.evaluate(...)
 
Share this answer
 
v2
Comments
DominoBoy 24-Oct-11 7:51am    
i changed to public static int evaluate( String output), but it says
modifier 'static' is only allowed in constant variable.
:(
Anthony Mushrow 24-Oct-11 10:30am    
Yeah I made a mistake, it's difficult to tell where everything is when nothing is indented. Like Philippe Mori said, you need to uncomment line 125 and use x.evaluate
An additional comments, format your code! The messier it is, the more mistakes you'll make.

Learn to put braces around EVERY block. In if, while, case and for blocks it is so easy to get this wrong, if you always brace, even for one line, you'll prevent a headache later.
Avoid putting multiple commands on a single line:
Java
// vile and ugly:
case '+':gotOper(ch, 1); break;

// readable and clear
case '+': {
    gotOper(ch, 1);
    break;
}


Notice the brace at the end of the line beginning the block, not on the next line.
If you use a proper IDE - Netbeans or Eclipse - you can set it to automatically provide the braces. I suggest you do so.

Another good practice is to use JavaDoc comments liberally and to write them BEFORE you write the method. This is good practice as you must write in English - or whatever you prefer - what the method will do before you add any code. After writing the code, then check that the JavaDoc is correct.

Your code, formatted and properly braced:
Java
package itp2;
import java.util.*;
import java.io.*;
class StackX {
    private int maxSize;
    private char[] stackArray;
    private int[] stackArray1;
    private int top;
     
    public StackX(int s) {
        maxSize = s;
        stackArray = new char[maxSize];
        top = -1;
    }
     
    public void push(char j) {
        stackArray[++top] = j;
    }
     
    public void push1(int j) {
        stackArray1[++top] =j;
    }
     
    public char pop() {
        return stackArray[top--];
    }
    
    public int pop1() {
        return stackArray[top--];
    }
    
    public char peek() {
        return stackArray[top];
    }
     
    public boolean isEmpty() {
        return (top == -1);
    }
     
    public int size() {
        return top+1;
    }
     
    public char peekN(int n) {
        return stackArray[n];
    }
     
     
 
} // end class StackX used from lab1
 
/** infix to postfix conversion borrowed from book */
class InToPost {
    private StackX theStack;
    private String input;
    private String output = "";
    
    /** constructor */
    public InToPost(String in) {
        input = in;
        int stackSize = input.length();
        theStack = new StackX(stackSize);
    }
    
    /** algorithm that does translation with cases */
    public String doTrans() {
        for(int j=0; j<input.length(); j++) {
        char ch = input.charAt(j);
         
            switch(ch) {
                case '+': {
                    gotOper(ch, 1);
                    break;
                }
                case '-': {
                    gotOper(ch, 1);
                    break;
                }
                case '*': {
                    gotOper(ch, 2);
                    break;
                }
                case '/': {
                    gotOper(ch, 2);
                    break;
                }
                default : {
                    output = output + ch;
                    break; //writes to output
                }
            } // end of switch
        } // end for loop
        // pop remaining operators at the end
        while( !theStack.isEmpty() ) 
        {
            output = output + theStack.pop(); // write to output
        }
        return output; // return postfix
    }
    
    /**--------------------------------------------------------------*/
    public void gotOper(char opThis, int prec1)
    {
        while (!theStack.isEmpty()) {
            char opTop = theStack.pop();
             
            if(1==1 ) {
                int prec2;
                
                // find new op prec
                if (opTop=='+' || opTop=='-') {
                    prec2 = 1;
                } else {
                    prec2 = 2;
                }
                if(prec2 < prec1) {
                    theStack.push(opTop);
                    break;
                } else {
                    output = output + opTop;
                }
            }
        } // end while loop
        theStack.push(opThis);
    }
}

public class ITP2 {
     
    public static void main(String[] args)
            throws IOException {
        String output;
        String input;
        while(true) {
            System.out.print("Enter infix: ");
            System.out.flush();
            input = getString();
            if( input.equals("") ) {
                break;
            }
         
            InToPost theTrans = new InToPost(input);
            output = theTrans.doTrans();
            //Eval x=new Eval();
             System.out.println("Evaluated expression: " + (output));
             System.out.println("Postfix is " + evaluate(output)) ;//// line 127
        }
    }

    public static String getString()
            throws IOException {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        String s = br.readLine();
        return s;
    }
    
    class Eval {
        private StackX operatorStack ;
        private StackX operandStack;
        private String output;
        Eval () {
        }

        public int evaluate( String output) {
            StringTokenizer s = new StringTokenizer(output);//divides into tokens
             
            int value;
            String symbol;
            while (s.hasMoreTokens()) {
                symbol = s.nextToken();
                // if its a number push it
                // if it's an operator, operate on the previous two popped operandStack items
                if (Character.isDigit(symbol.charAt(0))) {
                    Integer operand = new Integer(Integer.parseInt(symbol));
                    operandStack.push1(operand);
                } else {
                    int op2 = ((Integer)operandStack.pop1()).intValue();
                    int op1 = ((Integer)operandStack.pop1()).intValue();
                    int result = 0;
                    switch(symbol.charAt(0)) {
                        case '*': {
                            result = op1 * op2;
                            break;
                        }
                        case '+': {
                            result = op1 + op2;
                            break;
                        }
                        case '-': {
                            result = op1 - op2;
                            break;
                        }
                        case '/': {
                            result = op1 / op2;
                            break;
                        }
                        case '%': {
                            result = op1 % op2;
                            break;
                        }
                    }
                    Integer operand = new Integer(result);
                    operandStack.push1(operand);
                }
            }
            value = ((Integer)operandStack.pop1()).intValue();
            return value;
        }
         
    }        
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900