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
Solved

Open and Closing problem

Posted on 2001-06-11
6
187 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
fibonacci ten numbers 4 45
Java: anonymous class 4 29
Eclipse for Java EE development 2 27
What browser will run Java? 7 55
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

856 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