Home Answers Viewqa Java-Beginners How to match a string symbols using the INDEX and the method use Stack

 
 


jandi
How to match a string symbols using the INDEX and the method use Stack
1 Answer(s)      2 years and 6 months ago
Posted in : Java Beginners

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 Pages:
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
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
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
Logic Match Tag (<logic:match >...</logic:match >)
; match tag - We use this tag to evaluate the contents contained... at any position within the variable string. If specified, the match... position within the variable string. If specified, the match must occur
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
preg_match validate
preg_match validate  what is preg_match and how can i use... for matching the user input. And then use the preg_match method find the number... = ""; //Variable to hold user input //use preg_match to find no of matched
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
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
array, index, string
array, index, string  how can i make dictionary using array...please help
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
How to Print a Stack Trace Message
you leaned how to use the printStackTrace() method of Exception class... 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
Collection : Stack Example
pop() method. If you only want to retrieve top of stack element use peek...(). push(Object o) : This method pushes an element at the top of the stack. You can... { public static void main(String[] args) { Stack stack = new Stack(); // Pushing
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
Revisiting Stack Trace Decoding,java tutorial,java tutorials
stack depth. */ public String findCaller(int depth); public class...Revisiting Stack Trace Decoding 2003-01-28 The Java Specialists' Newsletter [Issue 063] - Revisiting Stack Trace Decoding Author: Dr. Heinz M. Kabutz
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
Java get Stack Trace
;  In this section, you will learn how to obtain the stack trace... to print out the exception stack trace to catch the error instead of using e.getMessage() method. By  the stack trace, you will know which method threw
Stack Overflow - Java Tutorials
Stack Overflow in Java A stack is the part of the memory. The local automatic variable is created on this stack and method arguments are passed. When a process starts, it get a default stack size which is fixed for each process. 
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
Find Character form String example
() method to find out the character in the string with the help of index position, and also find the character code using charCodeAt() method. User can see how... in Action Script3:- String maintain a index position for every character
Foreach loop with negative index in velocity
Foreach loop with negative index in velocity       This Example shows you how to use foreach loop with negative index in velocity. The method used in this example
How to Print a Stack Trace Message
How to Print a Stack Trace Message    ... using the printStackTrace() method that is the method of the Throwable class... java.lang.StringIndexOutOfBoundsException: String index out of range: 9
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
Body Mass Index (BMI)
void main(String[] args) { JFrame window = new JFrame("Body Mass Index... which disappear when the method or constructor returns. It's necessary to use...() method which takes a number and returns a String formatted as requested. See
Index Out of Bound Exception
of program 2.String Index Out of Bound Exception- The String Index Out of Bound.... Usually, An string object occurs out-of-range , when the index is less than zero, or greater than or equal to the length of the string. How to declare String
stack using linked list
stack using linked list  how to implement stack using linked list
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.... Example: A program  how to use charAt() method in java. import java.lang.
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 ** GIVEN STRING: a b c d e f g INPUT
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
Implementing a Stack in Java
;  In this section, you will learn how to implement a stack... are inserted and retrieved to/from the stack through the push() and pop() method... and push its onto the top of the stack. Stack.pop(): This is the method to removes
Java arraylist index() Function
Java arrayList has index for each added element. This index starts from 0. arrayList values can be retrieved by the get(index) method. Example of Java Arraylist Index() Function import
Use of getInt() method of ByteBuffer class in java.
Use of getInt() method of ByteBuffer class in java.  In this tutorial, we will see how to read integer value from byte buffer by using getInt() method... type Method Description static ByteBuffer allocate(int
Pointcut Example Using JdkRegexpMethod
intertesting thing is its pattern matching method names spring matches fully qualified method name. This is a powerful concept which allows you to match all the method within a given package. The example given below illustrates
Printing a Stack Trace to the Server Console
which method threw an exception and how that method is called. Instead of using..., you can use printStackTrace() method which prints a stack trace from... by 0. Instead of using e.getMessage(), we have use e.printStackTrace() so
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:- <... .style1 { font-size: medium; } String fromCharCode() method example
OGNL Index
the toCharArray() of the resulting string , After calling toCharArray() method... of OGNL : Access value of array using OGNL in struts2. How... and use. The most commonly used unit in OGNL expression language
string
string  how we conunt the no. of words in java without using in pre defined method in java
string
string  how do i extract words from strings in java???   Hi Friend, Either you can use split() method: import java.util.*; class ExtractWords { public static void main(String[] args) { Scanner input
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
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... value. matcher.start():-This method is used for finding the start index
Example of Java Stack Program
are using two methods of Stack. push(Object item): It pushes an item onto the top...;static void main(String[] args) {   Stack stack... Example of Java Stack Program     
java using Stack - Java Beginners
java using Stack  How convert decimal to binary using stack in java
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
String regionMatches()
the regionMatches() method of String class. We are going to use regionMatches() method...: This method tests if two string regions are equal or not. Here... String regionMatches()      
Stack Implementation java code - Java Beginners
Stack Implementation java code  Hello roseindia and java experts can u help me.can you give me a code for this sir/maam.thank you so much. STACK IMPLEMENTATION -expression evaluator *GIVEN a String of expression, create
length() Method In Java
;  In this section, you will learn how to use length() method of the String class. We are going to using length() method. These method... values. So we are using to length() method of the String class for the purpose
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... the string "india" into "INDIA" by using toUpperCase() method
currency symbols in excel
currency symbols in excel  How to add a currency symbol in Excel sheet? Please guide me
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

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.