3 Replies - 254 Views - Last Post: 05 November 2017 - 06:21 PM Rate Topic: -----

#1 aemaeth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 10-February 17

Java Stack "isBalanced" program to balanced parenthesis help?

Posted 05 November 2017 - 01:11 PM

This is what I have so far. I know how to write this with SWITCH cases and it runs correctly, but I wanted to write it using a while loop and if/else statements. Can anyone tell me whats wrong? it should be returning "balanced" or TRUE for "( ( ( ) )" string of chars because the algorithm is intentionally bugged (review question from book), but I don't think my actual code is correct and the loop is reading the input from the string. I didn't know how to make the loop find the end of the string or read each string input properly.

import java.util.Stack;

public class doWhileStackChar
{
  public static boolean isBalanced(String expresion)
  {
    // this program will check balance of "(" and ")" expressions only.
    final char leftNormal = '(';
    final char rightNormal = ')';
    
    
    Stack<Character> store = new Stack<Character>();
    int i;
    
    // while input has more characters
    while(expresion.isEmpty() != true)
    {
      for(i=0; i < expresion.length(); i++)
      {
        if(expresion.charAt(i) == leftNormal) // read character from input
        store.push(expresion.charAt(i)); // push on stack
        
        else if(expresion.isEmpty() != false)
          store.pop(); // pop character off stack
        else
          return false;
           }
  }
    return true;
}
}

This post has been edited by macosxnerd101: 05 November 2017 - 01:40 PM
Reason for edit:: Renamed "expression" to "expresion" because of expression bug on forums


Is This A Good Question/Topic? 0
  • +

Replies To: Java Stack "isBalanced" program to balanced parenthesis help?

#2 aemaeth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 10-February 17

Re: Java Stack "isBalanced" program to balanced parenthesis help?

Posted 05 November 2017 - 02:06 PM

I edited it up a bit and I think I'm close but when I compile, the compiler keeps runnning indefinitely and gives me some "memory error" in drjava

import java.util.Stack;

public class doWhileStackChar
{
  public static boolean isBalanced(String exp)
  {
   
    Stack<Character> stack = new Stack<Character>();
    int i = 0;
    
    // while input has more characters
    while(i < exp.length())
    {
       char c = exp.charAt(i);
       if(c == '(' )
         stack.push(c);
       
       else if(!stack.isEmpty())
         c = stack.pop(); // something might be wrong with this
       
       else
         return false;
    }
    return true;
  }
}

Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky  Icon User is online

  • Chinga la migra
  • member icon


Reputation: 10681
  • View blog
  • Posts: 18,292
  • Joined: 19-March 11

Re: Java Stack "isBalanced" program to balanced parenthesis help?

Posted 05 November 2017 - 03:16 PM

Your loop will keep running while i<exp.length()

Can that condition ever become false in the course of that loop?
Was This Post Helpful? 1
  • +
  • -

#4 aemaeth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 10-February 17

Re: Java Stack "isBalanced" program to balanced parenthesis help?

Posted 05 November 2017 - 06:21 PM

View Postjon.kiparsky, on 05 November 2017 - 03:16 PM, said:

Your loop will keep running while i<exp.length()

Can that condition ever become false in the course of that loop?


derp derp, I see, i forgot the i++ incrementator ;(

stupid me :(, thank you
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1