0

Hello, I'm trying to write a program that evaluates a postfix expression using the vector class. I have done this successfully using stacks but I am experiencing trouble with vectors.
Here is the error message I am recieving:

I:\vector.java:26: type Vector does not take parameters
public static int evalPostfix(String str, Vector<Object> opndVect)
^
I:\vector.java:79: type Vector does not take parameters
Vector<Object> myvect = new Vector<Object>();
^
I:\vector.java:79: type Vector does not take parameters
Vector<Object> myvect = new Vector<Object>();
^
3 errors

Process completed.

Here is my code:

import java.util.*;
public class vector 
{
public static int convert(char chValue)
{
	switch(chValue)
	{
		case '0': return 0;
		case '1': return 1;
		case '2': return 2;
		case '3': return 3;
		case '4': return 4;
		case '5': return 5;
		case '6': return 6;
		case '7': return 7;
		case '8': return 8;
		case '9': return 9;
		default:
		{
			System.out.println("Error in evaluation");
			return -1;
		}//terminates default
	}//terminates switch
}//terminates text of convert method
//begin coding for evalPostfix method
public static int evalPostfix(String str, Vector<Object> opndVect)
{
	for(int index=0; index<str.length(); ++index)
	{
		//if current char is an integer
		char chValue=str.charAt(index);
		if('0'<=chValue && chValue<='9')
			//add the converted integer digit to the operand vector
		opndVect.add(convert(chValue));
		else
		{
			int opnd1=opndVect.get(opndVect.lastElement());
			opndVect.remove(opndVect.lastElement());
			int opnd2=opndVect.get(opndVect.lastElement());
			opndVect.remove(opndVect.lastElement());
			switch(chValue)
			{
				case '+':
				{
					int value=opnd1+opnd2;
					opndVect.add(value);
					break;
				}//end case +
				case '*':
				{
					int value=opnd1*opnd2;
					opndVect.add(value);
					break;
				}//end case *
				case '-':
				{
					int value=opnd1-opnd2;
					opndVect.add(value);
					break;
				}//end case -
				case '/':
				{
					int value=opnd1/opnd2;
					opndVect.add(value);
					break;
				}//end case /
			}//terminates switch 
		}//terminates else
		System.out.println("Top of vector is now: " + opndVect.get(opndVect.lastElement()));
	}//terminates for loop
	System.out.println("Evaluation: " + opndVect.get(opndVect.lastElement()));
	int result=opndVect.get(opndVect.lastElement());
	opndVect.remove(opndVect.lastElement());
	return result;
}//terminates evalPostfix method
//begin coding of main method
public static void main(String[]args)
{
	Vector<Object> myvect = new Vector<Object>();
	System.out.println("Enter a string in postfix notation");
	Scanner scan = new Scanner(System.in);
	String str="";
	str=scan.next();
	evalPostfix(str, myvect);
}//terminates text of main method
}//terminates text of vector class

When I remnove the <Object> parameters from the constructor call I recieve this message:
I:\vector.java:26: type Vector does not take parameters
public static int evalPostfix(String str, Vector<Object> opndVect)
^
I:\vector.java:79: Vector is abstract; cannot be instantiated
Vector myvect = new Vector();
^
2 errors

Process completed.

Thanks in advance for any help.

3
Contributors
7
Replies
8
Views
8 Years
Discussion Span
Last Post by Grn Xtrm
1

Are you compiling against java 1.5 or later? Those declarations of Vector are perfectly valid post-1.5. You have other issues with using get(Object) calls that are not valid, but the declarations are fine.

Votes + Comments
Thanks for the help.
0

I am using Java 5.0.

You have other issues with using get(Object) calls that are not valid, but the declarations are fine.

Can you be more specific about my other issues? Thanks.

0

This call opndVect.get(opndVect.lastElement()); isn't valid because lastElement() returns an Object and get() requires an int index parameter. lastElement() itself returns the last element in that Vector, so really the get() is redundant anyway.

0

If you're using 5.0, then do you have a version of that class named "Vector" (in caps, not lower case) in that directory? Because, as James mentioned, you may have namespace collision problems.

0

Wow! I turns out he was right! I had a Vector.class file in the same directory. Deleting that solved the problem. Thanks again to both of you for the help.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.