Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



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

Have Programming Question? Ask it here!
 
 
 


jandi
How to match a string symbols using the INDEX and the method use Stack
1 Answer(s)      3 years and 9 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


Advertisement
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
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
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
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
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
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
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
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"
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
array, index, string
array, index, string  how can i make dictionary using array...please help
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
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
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
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 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
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
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
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
java using Stack - Java Beginners
java using Stack  How convert decimal to binary using stack in java
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
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
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 use string of one class into another Class
how to use string of one class into another Class  I created textField(JTextField t1=new JTextField();(it is in ClassA. The input of this textField i.e t1.getText() is to be used in different(another Class say in ClassB. How can
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
how to use toString method? - Java Beginners
how to use toString method?  i'm a new student, and learn with java. How to create a class named Employee with name and salary. Make a class... a method toString() that prints the manager?s name, department and salary. Make another
how to use sendRedirect in doGet method in jsp?
how to use sendRedirect in doGet method in jsp?  i am getting problem in jsp that when we are directly writing address to a jsp page in addressbar... how to do
How to use find method in Java - Java Beginners
How to use find method in Java  Hello, I want to write a class... properties. How can I do this? I was thinking, maybe get child objects of the browser class, then somehow parse through the class's objects/sub-objects, and use find
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
Use a tree stack to sort number in java?
Use a tree stack to sort number in java?  The Question is : Three stacks can be used to sort a list of numbers. Assuming stack in holds the input list of numbers, stack out is to hold the output list after sorting the numbers
Use a tree stack to sort number in java?
Use a tree stack to sort number in java?  The Question is : Three stacks can be used to sort a list of numbers. Assuming stack in holds the input list of numbers, stack out is to hold the output list after sorting the numbers
Use a tree stack to sort number in java?
Use a tree stack to sort number in java?  The Question is : Three stacks can be used to sort a list of numbers. Assuming stack in holds the input list of numbers, stack out is to hold the output list after sorting the numbers
Use a tree stack to sort number in java?
Use a tree stack to sort number in java?  The Question is : Three stacks can be used to sort a list of numbers. Assuming stack in holds the input list of numbers, stack out is to hold the output list after sorting the numbers
Use a tree stack to sort number in java?
Use a tree stack to sort number in java?  The Question is : Three stacks can be used to sort a list of numbers. Assuming stack in holds the input list of numbers, stack out is to hold the output list after sorting the numbers
Use a tree stack to sort number in java?
Use a tree stack to sort number in java?  The Question is : Three stacks can be used to sort a list of numbers. Assuming stack in holds the input list of numbers, stack out is to hold the output list after sorting the numbers
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
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.
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
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
Java Stack Example
Java Stack Example In this section we will read about how you can provide...() : This constructor is used to create an object of empty stack. Method Detail...() : This method is used to look the element at the top of the stack. It returns the top
How to use update and reset method of CRC32
How to use update and reset method of CRC32.In this tutorial, we will discuss about the update() and reset() method of CRC32 class. The CRC32 class... in  java.util.zip package .  In this example, you will see how
How to insert and retrieve Questions with special symbols and images into database in an ONLINE EXAMINATION project?
How to insert and retrieve Questions with special symbols and images... an online examination application. I am using JSP,MYSQL and ECLIPSE .The problem is " how can I insert the questions with some symbols and images into database
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
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
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
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
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
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... to convert in uppercase. toUpperCase() method is of string class. Description
how to match the key word from a text file
how to match the key word from a text file  p>Hi all, I have the code to match the key word and from the text. I have input like this reader.txt... java.util.regex.Pattern; class Classnew { public static String getnew() { String data2
 

 

 

DMCA.com