Solved

Open and Closing problem

Posted on 2001-06-11
6
185 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 105
JList custom Cell Renderer refresh 15 56
Java - Why doesn't this JFrame work 3 59
web application structure 18 77
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

770 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