How to match a string symbols using the INDEX and the method use Stack

How to match a string symbols using the INDEX and the method use Stack

How to match a string symbols using the INDEX and the method use Stack it will determine balance or not balance ...use the STACK ...and it must be in priority...... -PARENTHESIS -BRACKET -BRACES

Example output:

Enter a string symbols:{()}
BALANCED

Enter a string symbols: (((((((((((((()
NOT BALANCE

Enter a string symbols: {[()]}
BALANCED

Enter a string symbols: {{{[[()
NOT BALANCE

THE SYMBOLS MUST BE PAIR WITH EACH OTHER IN PARENTHESIS,BRACKETS AND BRACES

View Answers

November 29, 2010 at 5:14 PM

Hi Friend,

Try the following code:

import java.util.*;
public class Match{

public static boolean check(String s) {
Stack<Character> stack = new Stack<Character>();
char p1 = '(';
char p2 = ')';
char br1 = '{';
char br2 = '}';
char bk1 = '[';
char bk2 = ']';
for (int i = 0; i < s.length(); i++){
if (s.charAt(i) == p1) stack.push(p1); 
else if (s.charAt(i) == br1) stack.push(br1); 
else if (s.charAt(i) == bk1) stack.push(bk1);
else if (s.charAt(i) == p2){
if (stack.isEmpty())
return false;
if (stack.pop() != p1)
return false;
}
else if (s.charAt(i) == br2){
if (stack.isEmpty())
return false;
if (stack.pop() != br1)
return false;
}
else if (s.charAt(i) == bk2){
if (stack.isEmpty())
return false;
if (stack.pop() != bk1)
return false;
}
}
return stack.isEmpty();
}
public static void main(String args[]){
Scanner input = new Scanner(System.in);
System.out.print("Enter a string symbols: ");
String s = input.next();
if (check(s)== true)
System.out.println("BALANCED");
if (check(s)== false)
System.out.println("NOT BALANCED");
System.out.println();
}
}

Thanks









Related Tutorials/Questions & Answers:
How to match a string symbols using the INDEX and the method use Stack
How to match a string symbols using the INDEX and the method use Stack   How to match a string symbols using the INDEX and the method use Stack it will determine balance or not balance ...use the STACK
How to match a string using the INDEX and the method use Stack or Queue?
How to match a string using the INDEX and the method use Stack or Queue? ... of character that match the given data by INDEX position.using the STACK or QUEUE THE MATCH OF THE STRING IS BASED ON THERE INDEX POSITION **Example output
Advertisements
Java String using STACK - Java Beginners
Java String using STACK  Can you give me a code using Java String STACK using the parenthesis symbol ( ) it tells its Valid or Invalid it tells... the stack sample output Enter a string: ()() VALID Enter a string
iPhone String Match
iPhone String Match In this "String match" example we are going to use "rangeofstring" method, which is used to search for a string... of string to compare from, and using "rangeofString" method we can
How to convert a stack trace to a string?
of converting Stack Trace to String using Apache commons library: package... for the conversion of StackTrack error message into String. You can use any... Arrays class Apache commons stack trace to String Conversion of StackTrace
How to convert a stack trace to a string?
. Here is an example of converting Stack Trace to String using Apache commons... for the conversion of StackTrack error message into String. You can use any... Arrays class Apache commons stack trace to String Conversion of StackTrace
Reverse String using Stack
Reverse String using Stack You all are aware of Stack, a data structure... the string using these operations. Here is the code: import java.util.*; class StackReverseString { public static String revString(String str) { Stack
String length without using length() method in java
String length without using length() method in java  How to count length of string without using length() method in java?   Here is Example... the number of character into the given string without using the length() method
how can create album in java by using Stack ....
how can create album in java by using Stack ....  hi all , if i press push button put the image to the stack , and when i press pop button remove the image ..??? please help me please
Match string using regular expression without Case sensitivity
Match string using regular expression without Case sensitivity... the way to match the String using regular expression. For this we are going to make... Matching_Casesensitive.java are described below:- String regex="^java"
In data structure in java how to parse the given string and counts the member of character that match the given data?
In data structure in java how to parse the given string and counts the member... the given string and counts the member of character that match the given data.using the STACK or QUEUE Example output **ADS_TO_REPLACE_1 GIVEN STRING: a b c d e
Java get Stack Trace as String
Java get Stack Trace as String       In this section, you will learn how to get the stack trace...(printWriter)-  This method prints the throwable exception and its stack trace
stack using linked list
stack using linked list  how to implement stack using linked list
Finding start and end index of string using Regular expression
Finding start and end index of string using Regular expression... the way for finding start and end index of the string using regular expression...():-This method is used for finding the end index of the word starting with letter R
Interchanging String using RegularExpression
a String and interchange the index of string using Regularexpression. The steps... the string is to be splitted. sentence.split(splitPoint):-This method splits... Interchanging String using RegularExpression
array, index, string
array, index, string  how can i make dictionary using array...please help
JavaScript regex exec() method for text match.
JavaScript regex exec() method for text match.  JavaScript regex exec() method for text match.   <html> <head> <title>... it return null. You can use g modifier for global match as /Hello/g , i is used
match string to regular expression - Objective C
match string to regular expression - Objective C  How to match string value to regular expression in objective c?   Use NSPredicate. See the example given below... NSString *searchString = @"loginStatus"; NSString
charAt() method in java
the character at the given index within the string, index starting from 0. This method return the character at the specified index in the string. Syntax: public..._TO_REPLACE_1 Example: A program  how to use charAt() method in java
java using Stack - Java Beginners
java using Stack  How convert decimal to binary using stack in java...*; public class ConvertDecimalToBinaryUsingStack { public static void main(String... the number: "); int num = input.nextInt(); Stack stack = new Stack
String indexOf method example
.style1 { font-size: medium; } String indexOf() method example:- String class method indexOf() is use to find the starting character position of the substring from the string. The indexOf() method start to search from
Java Stack Example
; in java, how to use Stack in java. Before going into details we should know what... is empty or not and to check the size of stack size method, and to search the item in the stack and how far it is from top of stack Example : A Java code
String fromCharCode() method example in Action Script3
code using fromCharCode() method. we have define a string using this method. User can see how to use this method to find string. Example:-ADS_TO_REPLACE_1... .style1 { font-size: medium; } String fromCharCode() method example
java using Stack - Java Beginners
java using Stack  How convert decimal to binary using stack in java
How to split string in Java using comma?
How to split string in Java using comma?  Hi, Share the example code for splitting the string in Java using comma as separator. Thanks   Hi, You can use the split() function of the String class to split the string
How to use charAt() method in Java?
How to use charAt() method in Java?  Hi, what is the correct way of using chatAt() method in Java? Thanks   The charAt() method in Java is used to find the specific character at specific index. Check the tutorial
how to send spaces using get method.
how to send spaces using get method.  hi i want to know how to send spaces using get method? for example like http://localhost:8080/Sandeep?name... code: 1)form1.jsp: <html> <form method="get" action="form2.jsp"> <
how to count words in string using java
how to count words in string using java  how to count words in string using corejava   Hi Friend, Try the following code: import java.util.*; class CountWords{ public static void main(String[] args
String replaceFirst() Method
String replaceFirst() Method       In this program you will learn how to replace words in a text. We are going to use replaceFirst() method of String class in Java
Pointcut NameMethodMatch using BeanFactory
Pointcut NameMethodMatch using BeanFactory A pointcut is a bunch of codes... join points.Following is an example of NameMethodMatch using springConfig.xml.... { String name; public String getName() { return name; } public void
Stack
Stack  How to implement a stack using classes in java?   Here is an example that implements a Stack class based on arrays. public class Stack { private int top; private int[] storage; Stack(int
How to clean a buffer using clear method in java.
How to clean a buffer using clear method in java.      ..., we will discuss the use of clear() method. The Buffer class is a ... use, and after clear method the position will be zero and limit
Using get method in preferences
Using get method in preferences       In this section you will learn how to use the get() method... a simple way for using the get method in preferences. Get is the most useful
Count words in a string method?
Count words in a string method?  How do you Count words in a string...){ e.printStackTrace(); } } } Output: Enter string: hi how are you Number... for user input. split()method is used for splitting the given string according
Java Stack Example
will demonstrate you about how to use the stack collection in the Java...Java Stack Example In this section we will read about how you can provide...() : This method is used to check the availability of element in the stack. It returns true
String indexOf() Method
the indexOf() method of String class. We are going to use indexOf() method of String... String indexOf() Method      ... given below, we have taken a String. To find the index of any character we have
how insert multiple images in mysql database with use of struts 1.3.8 or java method, with single bean,or using array
how insert multiple images in mysql database with use of struts 1.3.8 or java method, with single bean,or using array  i am using netbeans 7.0 ,with struts 1.3.8 and i want to insert multiple images in mysql database ,with use
String indexOf() method in Java
String indexOf() method in Java In this tutorial we will discuss about indexOf() method in java. indexOf() method which is of string class,  used to locate a character or string.  The String class provide two method
Why we should use string args[] in main method in java?
Why we should use string args[] in main method in java?  we use only string in the main method not any other one.. specify the reason... and tell me... line from the command prompt   In the main method,the String array args
how to use String tokenizer on table that is retrieved from database using jsp servlet
how to use String tokenizer on table that is retrieved from database using jsp servlet  Query:Table---- mysql> select pid,medicinename,dose,day,qty from medicinedetails2 where pid=15
How to Print a Stack Trace Message
using the printStackTrace() method that prints the stack trace to the console... How to Print a Stack Trace Message  Debugging of Java program requires.... For this you can easily use the printStackTrace() method of Exception class. This method
how to convert xml string to soap request object using axis1.4?
how to convert xml string to soap request object using axis1.4?  how to convert xml string to soap request object using axis1.4
JDOM CDATA Example, Use of append(String str) method of CDATA class.
JDOM CDATA Example, Use of append(String str) method of CDATA class. In this tutorial, we will implement append() method of  CDATA class... with a string. The setContent(Content content ) method of Element class is used to set
Java toUpperCase() Method
Java toUpperCase() Method In Java In this section you will learn about how to use toUpperCase() in Java. From the name of the method you can also know what... contain the string to convert in uppercase. toUpperCase() method is of string
How you can escape a String for HTML using the StringEscapeUtils
Description: This tutorial demonstrate how you can escape a String for HTML using the StringEscapeUtils Here in this sample program it read the string variable and substitutes HTML entities using the StringEscapeUtils.escapeHtml
Java collection Stack example
Java collection Stack example  How to use Stack class in java... StackExample { public static void main(String [] args){ Stack stack = new... collection. We are using three methods of Stack. push(object items): The push
Search string using substring
Search string using substring  I want to print a string by searching a substring of that string. - i want to print email-ids from word files (resumes) in a directory. Search can be made using @ in the word file. So far i can
toUpperCase() Method In Java
;  In this section, you will learn how to use toUpperCase() method of the String class. We are going for using toUpperCase() method. This method... are using toUpperCase() method of the String class for the purpose. The following
How can we extract string 'roseindia.net ' from a string http://deepak@roseindia. net using regular expression of php?
How can we extract string 'roseindia.net ' from a string http://deepak@roseindia. net using regular expression of php?   How can we extract string 'roseindia.net ' from a string http://deepak@roseindia. net using regular
How ro convert char into string using recursive function in c#??
How ro convert char into string using recursive function in c#??  This coding is for java...but i want this in c# How ro convert char into string using recursive function in c#?? char [] ch={'H','E','L','L','o'}; TO ans

Ads