Vectors

Hello To All,

I have a program which check for matching braces or brackets. In this program, i am trying to implement a Stack using Vector. I am stuck with some errors. can anyone correct my errors and explain to me.Here goes my program:

import java.io.*;

//import java.util.io.*;

class StackVector

{

private Vector data = new Vector() ;

// public StackVector(int size)

// {

//data = new Vector(size);

// }

public StackVector(int size)

{

data = new Vector(size);

}

// post: removes all elements from stack

public void add(Object item)

{

data.addElement(item);

}

public void push(Object item)

{

add(item);

}

public Object remove()

{

Object result = data.elementAt(size()-1);

return result;

}

public Object pop()

{

return remove();

}

public Object peek()

{

return data.elementAt(size()-1);

}

public boolean empty()

{

return size() == 0;

}

public int size()

{

return data.size();

}

public void clear()

{

data.clear();

}

public boolean isEmpty()

{

return size() == 0;

}

} // end class StackVector

class Checker {

private String input;// input string

public Checker(String in)// constructor

{ input = in; }

public void check()

{

int stackSize = input.length();// get max stack size

StackVector theStack = new StackVector(stackSize);

// make stack

for(int j=0;j<input.length(); j++)

// get chars in turn

{

char ch = input.charAt(j);// get char

Character c = null;

switch(ch)

{

case '{': // opening symbols

case '[':

case '(':

theStack.push(new Character (c)); // push them

break;

case '}': // closing symbols

case ']':

case ')':

if(!theStack.isEmpty())

// if stack not empty,

{

char chx = theStack.pop(new Character(c)); // pop and check

if(

(ch=='}' && chx!='{') ||

(ch==']' && chx!='[') ||

(ch==')' && chx!='(') )

System.out.println("Error: "+ch+" at "+j);

}

else// prematurely empty

System.out.println("Error: "+ch+" at "+j);

break;

default:

// no action on other characters

break;

} // end switch

} // end for

// at this point, all characters // have been processed

if( !theStack.isEmpty() )

System.out.println("Error: missing right bracket");

} // end check()

} // end class Checker

class Brackets {

public static void main(String[] args) throws IOException

{

String input;

while(true)

{

System.out.print(

"Enter string containing brackets and braces: ");

System.out.flush();

input = getString();// read a string from kbd

if(input.equals(""))// quit if [Enter]

break;

Checker theChecker = new Checker(input);

theChecker.check();} // end while

} // end main()

public static String

getString() throws

IOException

{

InputStreamReader isr = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(isr);

String s = br.readLine();

return s;

}

} //end class Brackets

>

[3790 byte] By [amrita28] at [2007-9-26 1:25:00]
# 1

First of all you need to import more classes: java.util.Vector and java.lang.Character.

At least those 2.

The pop method you defined does not take an argument:

theStack.pop(new Character(c)); // pop and check

So either fix the call or the method.

Besides that, you are confusing the object Character and the primitive char. If you want the primitive from the object use c.charValue()

So when you construct a new Character from an existing one use :

new Character(c.charValue())

instead of

new Character(c);

I think that were the most errors. If it does what it's supposed to do when it compiles is up to you :)

esmo at 2007-6-29 1:05:55 > top of Java-index,Archived Forums,Java Programming...
# 2

Your remove method doesn't do what it should. Here's the correction:public Object remove() {

Object result = data.remove(size() - 1);

return result;

}

Here are other corrections:

o you need this import statement: import java.util.*;

o in the switch-statement in the check-method of the class Checker you have this line:theStack.push(new Character (c)); // push them

Change it to:theStack.push(new Character (ch)); // push them

(c is the Character, ch is the char)

Later in the same switch-statement you have this:char chx = theStack.pop(new Character(c)); // pop and check

Change it to:char chx = ((Character)theStack.pop()).charValue(); // pop and check

BTW: wouldn't it be easier to use the class java.util.Stack instead of implementing your own class?

jsalonen at 2007-6-29 1:05:55 > top of Java-index,Archived Forums,Java Programming...
# 3
Do you want to implement stacks ?check it out:java.util.Stack:^)
FelipeGaucho at 2007-6-29 1:05:55 > top of Java-index,Archived Forums,Java Programming...
# 4

here it comes a full source code.

import java.util.*;

public class Brackets

{

static final String initials = "{[(";

static final String terminals = "}])";

public static int checkFormula(String formula)

{

Stack brackets = new Stack();

for(int i=0; i<formula.length(); i++)

{

char symbol = formula.charAt(i);

if(initials.indexOf(symbol) > -1)

{

brackets.push(new Integer(i));

brackets.push(new Character(terminals.charAt(initials.indexOf(symbol))));

}

else if(terminals.indexOf(symbol) > -1)

{

if(((Character)brackets.pop()).compareTo(new Character(symbol)) != 0)

{

return i;

}

brackets.pop();

}

}

if( ! brackets.empty())

{

brackets.pop();

return ((Integer)brackets.pop()).intValue();

}

return -1;

}

static public void main(String[] arguments)

{

if(arguments[0] != null)

{

int test = checkFormula(arguments[0]);

if(test > -1)

{

System.out.println("Unclosed bracket at position "+test);

}

else

{

System.out.println("formula ok.");

}

}

}

}

FelipeGaucho at 2007-6-29 1:05:55 > top of Java-index,Archived Forums,Java Programming...
# 5
Hi, Thanks alot for the correction. I got what you try to meant.
amrita28 at 2007-6-29 1:05:55 > top of Java-index,Archived Forums,Java Programming...
# 6
Hello Joni Salonen , Thank You , very much.I got them corrected and you explained to me more clearly.Have a nice dayRegards,chandra
amrita28 at 2007-6-29 1:05:55 > top of Java-index,Archived Forums,Java Programming...
# 7

Hello Felipe Vieira Silva ,

very nice of you to show me a stack with full source code.

Very much appreciated.

I am very sorry that i cant assign any duke to you as i have already assigned to the other 2 people.

but in near future, i shall do some for you.

Thank very much, have a nice day.

regards,

chandra

amrita28 at 2007-6-29 1:05:55 > top of Java-index,Archived Forums,Java Programming...