Solved

Open and Closing problem

Posted on 2001-06-11
6
183 Views
Last Modified: 2010-03-31
hai, again with the java problem.
since i am new in java, i can only think of program algorithm
but cannot code them into java programming language.
what i had in mind is the following algorithm
which use to check for open and closing problem:

  //first, display "Pls enter string: " for user
  //get user input
  //store into string
  //declare a temp stack (string type)
  //set counter=0;

  //loop (check inside the string)
  //case'(', push ')'into temp stack,counter++
  //case'{', push '}'into temp stack,counter++
  //case'[', push ']'into temp stack,counter++
  //case'<', push '>'into temp stack,counter++
  //case symbol other than "({<[", counter++
  //
  //case')', check the top of stack of temp stack
  //if not match, then prompt error message with counter,incorrect and correct symbol
  //case'}', check the top of stack of temp stack
  //if not match, then prompt error message with counter,incorrect and correct symbol
  //case']', check the top of stack of temp stack
  //if not match, then prompt error message with counter,incorrect and correct symbol
  //case'>', check the top of stack of temp stack
  //if not match, then prompt error message with counter,incorrect and correct symbol
  //end loop

it will result like this:
Pls enter string: [{n-m>*c]
  //since the sixth element is wrong, should be '}'
  //so error message will be:

problem at position 5 (leftmost is 0)
the wrong closing is >, it should change to }

can someone help me with this?
0
Comment
Question by:JYnet
  • 3
  • 3
6 Comments
 

Accepted Solution

by:
surendraboppana earned 25 total points
ID: 6178517
it looks as if ur trying to convert ordinary expression to a postfix expression. I hope this code will certainly solve the problem.

import java.io.*;          // need this for  reading input
import java.util.Stack;    // need this for Stack class  
class Postfix
{
   public void main(String args[])throws IOException
   {
     //defining a buffered reader to read from keyboard

     BufferedReader br=new BufferedReader(new InputReader (System.in));
     Stack stk;
     String temp;
      do
       {
        temp=br.readLine();
       //reads a line from the input till the Return key
       //and stores it as a string in temp
       }while(!temp.equals("!"));

       //reads input from the keyboard until some symbol
      //in this case ! is read

      for(int i=0;i<temp.length-2;i++)
      {
          char c=temp.charAt(i);
           
           switch(c)
           {
              case'(':stk.push('}');
              //push is already defined in java
               .
               .

               .
              case'}':if(!stk.pop.equals("{")
                       System.out.println("Error");
              //print error message if the popped element
              //is not {;

              .
              .

              .
            }
      }
}
give input like this

{3*9)}-[4*2] (enter)
!(enter)

ouput

Error              



       This code i myself have used. so this is bound to work.all the best.any problem let me know.


      regards

   Surendra
0
 

Author Comment

by:JYnet
ID: 6179703
i have tried the following source code and modify something,
but when i debug, it keep saying that :
1. attempt reference method length in class java.lang.String as an instance variable
2. attempt reference method pop in class java.util.Stack as an instance variable
3. incompatible type for method. can't convert char to java.lang.Object

here is the code:
import java.io.*;
import java.util.Stack;
import java.lang.*;

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

  BufferedReader br=new BufferedReader(new InputStreamReader (System.in));
  Stack stk =null;
  String temp;
  do{
    temp=br.readLine();
    //reads a line from the input till the Return key
    //and stores it as a string in temp
  }while(!temp.equals(" "));

  for(int i=0;i < (temp.length-2);i++){
    char c=temp.charAt(i);
                               
    switch(c){
    case'(':stk.push(')');
    case'[':stk.push(']');
    case'{':stk.push('}');
    case'<':stk.push('>');

    //case other than {}[]<>()

    case')':if(!stk.pop.equals(')'))
      System.out.println("Error at"+i+ "Pls change " + ")" + "to "+stk.pop);
      break;
    case']':if(!stk.pop.equals(']'))
      System.out.println("Error at"+i+ "Pls change " + "]" + "to "+stk.pop);
      break;
    case'}':if(!stk.pop.equals('}'))
      System.out.println("Error at"+i+ "Pls change " + "}" + "to "+stk.pop);
      break;
    case'>':if(!stk.pop.equals('>'))
      System.out.println("Error at"+i+ "Pls change " + ">" + "to "+stk.pop);
      break;
    }
  }
  }
}

one more thing is to check for any characters beside "{}[]()<>",
any code to make it run?
0
 

Author Comment

by:JYnet
ID: 6184787
can anyone use the source code to test, and tell me what's the problem?
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Expert Comment

by:surendraboppana
ID: 6185194
hi JYnet

  u haven't initialised stk

Stack stk=new Stack();


while using push methods dom like this


stk.push(new Character('{'));//used to wrap the chracter to
                             //an Object


and for length use   temp.length();//its a method

this will work for sure
0
 

Author Comment

by:JYnet
ID: 6197328
i think now come to the "pop" part. the same problem comes out.

  case')':if(stk.peek()!=')')
    System.out.println("Error at"+i+ "Pls change )" + "to " + stk.pop());
    break;

what "bug" is inside and
will it work for jdk1.2?
0
 

Expert Comment

by:surendraboppana
ID: 6197692
hi JYnet

       stk.pop() not stk.pop(}).it doesn't take any arguments


          bye
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now