Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Method Calls, UGENT!

Status
Not open for further replies.

Matty2

IS-IT--Management
Oct 7, 2003
1
CA
Hi i'm making a program to see if parenthesis are balanced, only one kind ( & ) , ok let's say you type in (()), it will be balaced. This will be easier to explain if i attach some of the code: ( I PUT THE SPOT IN THE CODE IN CAPS WHERE I NEED HELP) thanks


public class test3{


public static int top;
public static String[] s;

public static void main(String args[]){


String s1;
String s2 = "(";
s = new String[5];
top = 0;

s1 = JOptionPane.showInputDialog("Enter Expression");

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

if(s1.charAt(j) == '(')
{

BASICALLY RIGHT HERE I WANT TO SEND '(' TO THE METHOD
PUSH WITH OUT RETURNING ANYTHING, HOW DO I DO THAT??
}



if(s1.charAt(j) == ')')
{
// GO to pop method

}

}


}



public void push(String ob)
{ if ( top == s.length )
{ // array is full---create a new one to hold more objects:
String[] temp = new String[s.length * 2];
for ( int i = 0; i != top; i = i+1 )
{ temp = s; } // copy elements into temp
s = temp; // set s to hold address of temp
}
s[top] = ob;
top = top + 1;
}


Thanks []v[]attyD
 
&quot;BASICALLY RIGHT HERE I WANT TO SEND '(' TO THE METHOD
PUSH WITH OUT RETURNING ANYTHING, HOW DO I DO THAT??&quot;

I couldn't get your problem clearly I'm afraid; but if you want to use your '(' character, why don't you add an argument to your push method such as

public void push(String ob,char p)

Or am I in the wrong track?If so please tell us.


Salih Sipahi
Software Engineer.
City of Istanbul Turkey
s.sipahi@sahinlerholding.com.tr
turkey_clr.gif
 
Hi
I'm not sure what you mean, maybee the following code
will fix it ?
It will return true or false, depending if the ( and ) are balanced.


[JAVA]
public class Check
{

public static boolean BalanceExist(String data)
{
int left = 0;
int right = 0;

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

if(data.charAt(i)=='(')
{
left++;
}
else if(data.charAt(i)==')')
{
right++;
}
}

return (left==right);
}


}
[/JAVA]
 
Matty, I suspect that your problem is because main is static (as it has to be) but push is not. Therefore to call push from main you need an instance of your class.
There are two ways to get round this:
1. At the start of main, put
Test3 myTest3 = new Test3(); //create an instance of test3
....
then you can use
myTest3.push( &quot;(&quot; );

2. alternatively, and more simply, just make push and pop static as well. public static void push( String os )...
then you can just write
push( &quot;(&quot; );

Of course, if all you want to do is count brackets then
FrederikN's method is very straightforward. If as I suspect you are gearing up towards writing a more complete parser -- maybe to output reverse polish -- then push and pop is the way to go.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top