Solved

Open and Closing problem

Posted on 2001-06-11
6
190 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

615 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