I am getting errors in my coding which state that the class, interface or enum is expected and I have review my codes and cannot find any missing ending braces or misspelled words. Can some review my codes to see if there is something that I am missing
import java.util.Stack;
import java.util.EmptyStackException;
import java.util.Scanner;
class Arithmetic
{
Stack stk;
String expression, postfix;
int length;
Arithmetic (String s)
{
this.expression = s;
postfix = " ";
this.length = expression.length();
stk = new Stack();
}
//Validate the expression-make sure parentheses are balanced
boolean isBalance()
{
int i = 0;
boolean fail = false;
try
{
while (i < length && !fail)
{
char s = expression.charAt(i);
switch(s)
{
case Constants.LEFT_NORMAL:
stk.push(new Character(s));
break;
case Constants.RIGHT_NORMAL:
stk.pop();
break;
default:
//Skip all othe token
break;
}//end of switch
i++;
}//end of while
}//end of try
catch (EmptyStackException e)
{
fail = true;
}
if(stk.empty()&& !fail)
return true;
else
return false;
}
}
//Convert expression to postfix notation
void postfixExpression()
{
Scanner scan = new Scanner(expression);
char current;
while (scan.hasNext())
{
String token = scan.next();
if(isNumber(token))//Bullet #1
postfix = postfix + token + Constants.A_SPACE;
else
{
current = token.charAt(0);
if(isParentheses(current))//Bullet #2 begins
{
if(stk.empty() || current==Constants.LEFT_NORMAL)
{
stk.push(new Character(current));
}
else if (current==Constants.RIGHT_NORMAL)
{
try
{
Character ch = (Character)stk.pop();
char top = ch.charValue();
while(top != Constants.LEFT_NORMAL)
{
postfix = postfix + top + Constants.A_SPACE;
ch = (Character)stk.pop();
top = ch.charValue();
}
}
catch (EmptyStackExpression e)
{
}
}
}//End bullet #2
else if(isOperator (current))//Bullet #3 begins
{
if(stk.empty())
{
stk.push(new Character(current));
}
else
{
try
{
char top = (Character)stk.peek();
boolean higher = hasHigherPrecedence(top, current);
while (top != Constants.LEFT_NORMAL && higher)
{
postfix = postfix + stk.pop() + Constants.A_SPACE;
top = (Character)stk.peek();
}
stk.push(new Character(current));
}
catch(EmptyStackException e)
{
stk.push(new Character(current));
}
}
}//Bullet #3 ends
}
}//Outer loop ends
try
{
while(!stk.empty())//Bullet #4
{
postfix = postfix + stk.pop()+ Constants.A_SPACE;
}
}
catch(EmptyStackException e)
{
}
}//End postfixExpression
boolean isNumber(String str)
{
boolean number = true;
try
{
Integer.parseInt(s);
}
catch(NumberFormatException e)
{
number = false;
}
return number;
}
boolean isParentheses(char current)
{
return((ch==Constants.LEFT_NORMAL)||(ch==Constants.RIGHT_NORMAL));
}
boolean isOperator(char ch)
{
boolean operator;
switch(ch)
{
case Constants.MULTIPLICATION:
case Constants.DIVISION:
case Constants.ADDITION:
case Constants.SUBTRACTION:
operator = true;
break;
default:
operator = false;
break;
}
return operator;
}
boolean hasHigherPrecedence(char top, char current)
{
boolean higher;
switch(top)
{
case Constants.MULTIPLICATION:
case Constants.DIVISION:
switch(current)
{
case Constants.ADDITION:
case Constants.SUBTRACTION:
higher = true;
break;
default:
higher = false;
break;
}
break;
default:
higher = false;
break;
}
return higher;
}
//Evaluate RPN
void evaluateRPN()
{
stk.clear();
Scanner scan = new Scanner(postfix);
char current;
while(scan.hasNext())
{
String token = scan.next();
if(isNumber(token))
stk.push(new Integer(token));
else
{
current = token.charAt(0);
if(isOperator(current))
{
try
{
Integer x1 = (Integer)stk.pop();
int t1 = x1.intValue();
Integer x2 = (Integer)stk.pop();
int t2 = x2.intValue();
switch(current)
{
case Constants.ADDITION:
stk.push(new Integer(t2 + t1));
break;
case Constants.SUBTRACTION:
stk.push(new Integer(t2 - t1));
break;
case Constants.MULTIPLICATION:
stk.push(new Integer(t2 * t1));
break;
case Constants.DIVISION:
stk.push(new Integer(t2 / t1));
break;
default:
break;
}
}
catch (EmptyStackExpression e)
{
}
catch(arithmeticException e)
{
System.out.println(e.toString());
result = "divide by zero - error.";
}
catch(NumberFormatException e)
{
}
boolean sizeIsOne()
{
return (stk.size()==1);
}
int getResult()
{
Integer i = (Integer)stk.pop();
return intValue();
}
String getpostFixExpression()
{
return postFix;
}
}
}
}
}
6 Replies - 1639 Views - Last Post: 17 July 2011 - 05:16 PM
#1
java, error that class, interface or enum is expected
Posted 17 July 2011 - 04:29 PM
Replies To: java, error that class, interface or enum is expected
#2
Re: java, error that class, interface or enum is expected
Posted 17 July 2011 - 04:42 PM
It's because you're ending the parenthesis for your class before postfixExpression() method. Delete the parenthesis before that method and add one to the last line of your code and it should get rid of this error. Also I'm not sure if the code editor added this or if this is actually in your code but exp<b></b>ression is not a valid variable name in java. You need to get rid of the <, >, and /.
#3
Re: java, error that class, interface or enum is expected
Posted 17 July 2011 - 04:42 PM
It would help a lot if you wrote the line number
#4
Re: java, error that class, interface or enum is expected
Posted 17 July 2011 - 04:55 PM
giggly kisses, on 17 July 2011 - 04:42 PM, said:
It's because you're ending the parenthesis for your class before postfixExpression() method. Delete the parenthesis before that method and add one to the last line of your code and it should get rid of this error. Also I'm not sure if the code editor added this or if this is actually in your code but exp<b></b>ression is not a valid variable name in java. You need to get rid of the <, >, and /.
code editor inserted those keys, they are not part of my code.
#5
Re: java, error that class, interface or enum is expected
Posted 17 July 2011 - 05:06 PM
I figured, well you still need to move your bracket (I said parenthesis in my last post, I ment bracket) from in front of your postfixExpression() method to the last line of your program.
#6
Re: java, error that class, interface or enum is expected
Posted 17 July 2011 - 05:06 PM
Your advice did work, thank you so much!
#7
Re: java, error that class, interface or enum is expected
Posted 17 July 2011 - 05:16 PM
Good to hear, hope the rest of the project is smooth sailing.
Page 1 of 1

New Topic/Question
Reply



MultiQuote


|