Hello, I'm facing another problem with vectors, this time with the problem of converting infix to postfix. The compile process completes, but I recieve an error after I enter the infix expression. Here is the exact error I recieve:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 43 >= 2
at java.util.Vector.removeElementAt(Vector.java:511)
at vector4.InToPost(vector4.java:22)
at vector4.main(vector4.java:79)

Process completed.

I don't really know what is wrong with my code and why I am recieving the error. Here is my code:

import java.util.*;
public class vector4
{
	public static String InToPost(String infixString)
	{
		//operator vector initialized
		Vector<Character> opVect = new Vector<Character>();
		//postfixString initialized as empty
		String postfixString = "";
		//scan infix strring and take appropriate action
		for(int index = 0; index<infixString.length(); ++index)
		{
			char chValue = infixString.charAt(index);
			if(chValue == '(')
				opVect.add('(');
			else if(chValue == ')')
			{
				Character oper = opVect.lastElement();
				while(!(oper.equals('(')) && !(opVect.isEmpty()))
				{
					postfixString += oper.charValue();
					opVect.removeElementAt(opVect.lastElement());
					oper = opVect.lastElement();
				}//end while loop
			opVect.removeElementAt(opVect.lastElement());
			}//end else if 
			else if(chValue == '+' || chValue == '-')
			{
				if(opVect.isEmpty()) //operatorStack is empty
				opVect.add(chValue);
				else //current operatorStack is not empty
				{
					Character oper = opVect.lastElement();
					while(!(opVect.isEmpty() || oper.equals(new Character('(')) || oper.equals(new Character(')'))))
					{
						opVect.removeElementAt(opVect.lastElement());
						postfixString += oper.charValue();
					}//ends while loop
				opVect.add(chValue);
				}//end else 
			}//end else if
			else if(chValue == '*' || chValue == '/')
			{
				if(opVect.isEmpty())
					opVect.add(chValue);
				else
				{
					Character oper = opVect.lastElement();
					while(!oper.equals(new Character('+')) && !oper.equals(new Character('-')) && !opVect.isEmpty())
					{
						opVect.removeElementAt(opVect.lastElement());
						postfixString += oper.charValue();
					}//end while loop
				opVect.add(chValue);
				}//end else
			}//end else if
			else
				postfixString += chValue;
		}//end for loop
	while(!opVect.isEmpty())
	{
		Character oper = opVect.lastElement();
		if(!oper.equals(new Character('(')))
		{
			opVect.removeElementAt(opVect.lastElement());
			postfixString += oper.charValue();
		}//end if
	}//end while
	return postfixString ;
}//terminates text of InToPost method

public static void main(String[]args)
{
	Vector<Character> myvect = new Vector<Character>();
	System.out.println("Enter a string in infix notation");
	Scanner scan = new Scanner(System.in);
	String str="";
	str=scan.next();
	InToPost(str);
	System.out.println(str);
}//terminates text of main method
}//terminates text of vector4 class

Thanks in advance for any help.

Recommended Answers

All 7 Replies

You're trying to access element 43 of a vector that only contains 2 elements.

Also, this code opVect.removeElementAt(opVect.lastElement()); is using an object parameter, lastElement(), instead of an int index parameter for removeElementAt(). You are doing that in several places and really the only reason it even compiles is a nuance of auto-boxing and upcasting of char. You need to supply an index parameter for those calls or use remove(Object) instead of removeElementAt(int).

What line(s) of my program make the compiler think that I'm trying to access the 43rd element?
Also, I want to remove the last element of the vector to simulate a pop for stacks. How can I accomplish that?
Thanks for the reply.

To know where the last item is, your program can either keep track of the size of the Vector, or you can use the Vector class's size() method.

Definitely use size(), why do it yourself?

I'm trying to use size() but I am getting incompatible type errors like these:
G:\Java 3\vector4.java:18: incompatible types
found : int
required: java.lang.Character
Character oper = opVect.size();
^
G:\Java 3\vector4.java:23: incompatible types
found : int
required: java.lang.Character
oper = opVect.size();
^
G:\Java 3\vector4.java:33: incompatible types
found : int
required: java.lang.String
String oper = opVect.size();
^
Any suggestions on how to resolve this?
Thanks alot for your time.

size(), as it says in the API doc, returns an integer, which is the number of elements in the Vector. It doesn't return the element itself.
To get an element you need elementAt(int). Since elements are numbered from 0, the last one is number (size-1).
ps Having said all that - have a look at the lastElement() method for Vectors.

Hi, I managed to resolve the problem by using

remove(0)

to remove the first element in the vector. Thanks again for taking the time to help me out.

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.