Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6581595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:10:23+00:00 2026-05-25T16:10:23+00:00

I’m writing a program that will take in an equation and check if all

  • 0

I’m writing a program that will take in an equation and check if all the parentheses line up and it will output if it is good or not.

For Ex: (3+4) is good
((3*8) is NOT Good

I’m not allowed to use java’s built in push() pop() methods ext..
I have to make my own which I think I got….I think!
The problem I’m having is in the Test() method.

First I’m not sure how to write the while loop like:

while(there are still characters)

Anyway the output I’m getting is: stack is empty -1

Any help is appreciated. I’m one of the slower program learners and I couldn’t be trying any harder. Thanks.

Here’s what I got:

public class Stacked {

  int top;
  char stack[];
  int maxLen;

  public Stacked(int max) {
    top = -1; 
    maxLen = max; 
    stack = new char[maxLen];
  }

  public void push(char item) {
    top++;
    stack[top] = item;
  }

  public int pop() {
    //x = stack[top];
    //top = top - 1;
    top--;
    return stack[top];
  }

  public boolean isStackEmpty() {
    if(top == -1) {
      System.out.println("Stack is empty" + top);
      return true;
    } else 
      return false;
  }

  public void reset() { 
    top = -1;
  }

  public void showStack() {
    System.out.println(" ");
    System.out.println("Stack Contents...");
    for(int j = top; j > -1; j--){
      System.out.println(stack[j]);
    }
    System.out.println(" ");
  }

  public void showStack0toTop() {
    System.out.println(" ");
    System.out.println("Stack Contents...");
    for(int j=0; j>=top; j++){
      System.out.println(stack[j]); 
    }
    System.out.println(" ");
  }
//}

  public boolean test(String p ){
    boolean balanced = false;
    balanced = false;
    //while (  ) 
    for(char i = '('; i < p.length(); i++ ){
      push('(');
    }
    for (char j = ')'; j < p.length(); j++){
      pop();
    }
    if (isStackEmpty()) {
      balanced = true;
      //return balanced;
    }
    return balanced;
  } 

  public static void main(String[] args) {
    Stacked stacks = new Stacked(100);
    String y = new String("(((1+2)*3)");

    stacks.test(y);
    //System.out.println(stacks.test(y));
  }    
}

Now I’m getting somewhere. I need to be pointed in the right direction again. Thanks everyone this helped big time. I still have a lot more to do but this is good for now. Eventually I need to create a two more methods: one “infix to postfix” and the other “evaluating postfix” and at the end I’ll need to read in answers from a text file instead of putting my own into the main method. Thanks again much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:10:23+00:00Added an answer on May 25, 2026 at 4:10 pm

    Unless you need to actually evaluate the equation, a stack is too complicated a solution here. You simply need a counter:

    int openParentheses = 0;
    
    for (int i = 0; i < p.length(); i++) {
        if (p.charAt(i) == '(') {
            openParentheses++;
        } else if (p.charAt(i) == ')') {
            openParentheses--;
        }
    
        //check if there are more closed than open
        if (openParentheses < 0) {
            return false;
        }
    }
    
    if (openParentheses == 0) {
        return true;
    } else {
        return false;
    }
    

    If you absolutely must use stacks, use this:

    for (int i = 0; i < p.length(); i++) {
        if (p.charAt(i) == '(') {
            push('x'); //doesn't matter what character you push on to the stack
        } else if (p.charAt(i) == ')') {
            pop();
        }
    
        //check if there are more closed than open
        if (stackIsEmpty()) {
            return false;
        }
    }
    
    if (isStackEmpty()) {
        return true;
    } else {
        return false;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I have a text area in my form which accepts all possible characters from
I am writing an app with both english and french support. The app requests
I'm trying to create an if statement in PHP that prevents a single post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.