Need some help fixing the errors in my code, im TRYING to make a math expression type of program, using stacks. Was running fine, then i wake up in the morning and my code has like 15compiler errors, would appreciate any help!!

import java.util.*;
import java.io.*;

public class d8 {


public static void main(String[] args) throws FileNotFoundException {

BufferedReader br = new BufferedReader(new FileReader(args[0]));

eval(br);

br.close();
}

public static void apply(GenericStack os, GenericStack vs){

int v1, v2, r;
char op;

op = os.pop();
v2 = vs.pop();
v1 = vs.pop();

/* printf("Applying %d %c %d...\n", v1, op, v2); /*debug*/

switch (op) {

case '+':
vs.push(v1 + v2);
break;

case '-':
vs.push(v1 - v2);
break;

case '/':
vs.push(v1 / v2);
break;

case '*':
vs.push(v1 * v2);
break;

case '%':
vs.push(v1 % v2);
break;
} /* switch */

} /* apply */

public static void eval(Bufferedreader src) throws IOException {

int x, num;
GenericStack ostack = new GenericStack();
GenericStack vstack = new GenericStack();
int c;

while ( (c = src.raed()) != -1 ) {

if ( c != '\n' && !Character.isDigit(c) )
System.out.printf("%c", c);

switch(c) {

case '(':
ostack.push((char)c);
break;

case ')':
/* dumpstack(ostack, 0);*/
while ( ostack.top() != '(' )
apply(ostack, vstack);
ostack.pop();
break;

case '0': case '1': case 2: case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
num = 0;
while ( Character.isDigit(c) ) {
/* printf("digit=%c\n", c); /*debug*/
System.out.printf("%c", c);
num = num * 10 + (c - '0');
src.mark(1);
c = src.read();
}
src.reset();
/* printf("num=%d\n",num);*/
vstack.push(num);
/* dumpstack(vstack,1);*/
break;

case '+': case '-':
case '*': case '/': case '%':
if ( !ostack.isEmpty() )
while ( !ostack.isEmpty() && prec(ostack.top()) >= prec((char)c))
apply(ostack, vstack);
ostack.push((char)c);
break;

case ' ': case '\t':
break;

case '\n':
while ( ! ostack.isEmpty() )
apply(ostack, vstack);

System.out.printf(" = %d.\n", vstack.pop());
break;
} /* switch */

} /* while */

} /* eval */

public static int prec (char c) {

switch (c) {

case '+':
case '-':
return(0);
case '*':
case '/':
case '%':
return(1);
case '(':
return(-1);
} /* switch */

return (-1);
} /* prec */

}
import java.util.*;

public class GenericStack {

private Node top;

private class Node {

private T data;
private Node next;

public Node(T item) {
data = item;
next = null;
}

}

public GenericStack () {
top = null;
}

public boolean isEmpty() {
return top = null;
}

public T top() {
return top.data;
}

public void push(t item) {
Node n = new Node(item);

n.next = top;
top = n;

}

public T pop() throws EmptyStackException{

if ( top == null )
throw new EmptyStackException();

T n = top.data;
top = top.next;

return n;
}
}

Recommended Answers

All 4 Replies

Need some help fixing the errors in my code,

Please post the full text of the error messages if you need help with them.

d8.java:16: error: unexpected type
    public static void apply(GenericStack<char> os, GenericStack<Integer> vs){
                                          ^
  required: reference
  found:    char
d8.java:16: error: type GenericStack does not take parameters
    public static void apply(GenericStack<char> os, GenericStack<Integer> vs){
                                         ^
.java:16: error: type GenericStack does not take parameters
    public static void apply(GenericStack<char> os, GenericStack<Integer> vs){
                                                                ^
d8.java:52: error: cannot find symbol
    public static void eval(Bufferedreader src) throws IOException {
                            ^
  symbol:   class Bufferedreader
  location: class d8
.\GenericStack.java:5: error: cannot find symbol
    private Node <T> top;
                  ^
  symbol:   class T
  location: class GenericStack
.\GenericStack.java:27: error: cannot find symbol
    public T top() {
           ^
  symbol:   class T
  location: class GenericStack
.\GenericStack.java:31: error: cannot find symbol
    public void push(t item) {
                     ^
  symbol:   class t
  location: class GenericStack
.\GenericStack.java:39: error: cannot find symbol
    public T pop() throws EmptyStackException{
           ^
  symbol:   class T
  location: class GenericStack
d8.java:55: error: type GenericStack does not take parameters
        GenericStack<Character> ostack = new GenericStack<Character>();
                    ^
d8.java:55: error: type GenericStack does not take parameters
        GenericStack<Character> ostack = new GenericStack<Character>();
                                                         ^
d8.java:56: error: type GenericStack does not take parameters
        GenericStack<Integer> vstack = new GenericStack<Integer>();
                    ^
d8.java:56: error: type GenericStack does not take parameters
        GenericStack<Integer> vstack = new GenericStack<Integer>();
                                                       ^
.\GenericStack.java:24: error: incompatible types
        return top = null;
                   ^
  required: boolean
  found:    GenericStack.Node<T>
.\GenericStack.java:32: error: cannot find symbol
        Node<T> n = new Node<T>(item);
             ^
  symbol:   class T
  location: class GenericStack
.\GenericStack.java:32: error: cannot find symbol
        Node<T> n = new Node<T>(item);
                             ^
  symbol:   class T
  location: class GenericStack
.\GenericStack.java:44: error: cannot find symbol
         T n = top.data;
         ^
  symbol:   class T
  location: class GenericStack
16 errors

Tool completed with exit code 1

The text of the source statement in the error message is different than what you posted.
Why is that?
The posted source:
public static void apply(GenericStack os, GenericStack vs){

from the error message:
public static void apply(GenericStack<char> os, GenericStack<Integer> vs){

Member Avatar for hfx642

As well...
1. It thinks you have a class T, yet... I don't see a class T.
2. Spelling of BufferedReader is incorrect (Case Sensitive).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.