Solved

Open and Closing problem

Posted on 2001-06-11
6
188 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet filter example 37 83
Tomcat: Unable to run tomcat service. 2 29
JAVA API design with micro service cloud in mind 1 76
how to debug htl and js pages 8 53
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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.

763 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