using stacks and arrays
Given problem:
String Input: {this[is(just)a]test}
Elements inside the curly braces: [t, h, i, s, [, i, s, (, j, u, s, t, ), a, ], t, e, s, t]
Elements inside the brackets: [i, s, (, j, u, s, t, ), a]
Elements inside the parentheses: [j, u, s, t]
output of my program:
String Input: {this[is(just)a]test}
Elements inside the curly braces: [t, h, i, s, [, i, s, (, j, u, s, t, ), a, ], t, e, s, t]
Elements inside the brackets: [i, s, (, j, u, s, t, ), a, i, s, (, j, u, s, t, ), a, i, s, (, j, u, s, t, ), a, i, s, (, j, u, s, t, ), a, i, s, (, j, u, s, t, ), a, i, s, (, j, u, s, t, ), a]
Elements inside the parentheses: [j, u, s, t, j, u, s, t, j, u, s, t, j, u, s, t, j, u, s, t, j, u, s, t, j, u, s, t, j, u, s, t]
nag.balik2x ang output sa Elements inside the brackets ugElements inside the parentheses:
here is my source codes:
Code:import java.util.*; public class Brackets { private String input; private char [] array; private int index1=0,index2=0; private static Scanner in = new Scanner(System.in); private static Stack myStack = new Stack(); public Brackets(){ askInput(); Process(); } private void askInput(){ System.out.println("String Input:"); setInput(in.nextLine()); setCharArray(new char [getInput().length()]); } private void Process(){ for(int x =0; x<getInput().length();x++){ char i = getInput().charAt(x); array[x] = i; } InsideTheCurlyBraces(); InsideTheBrackets(); InsideTheParentheses(); } private void InsideTheCurlyBraces(){ for(int x=0; x<getCharArray().length;x++){ if(array[x]=='{'){ index1 =x; } if(array[x]=='}'){ index2 = x; } for(int y=index1+1; y<index2; y++){ myStack.push(array[y]); } } System.out.println("Elements inside the curly braces: "+myStack); myStack.clear(); index1 =0; index2 =0; } private void InsideTheBrackets(){ for(int x=0; x<getCharArray().length;x++){ if(array[x]=='['){ index1 =x; } if(array[x]==']'){ index2 = x; } for(int y=index1+1; y<index2; y++){ myStack.push(array[y]); } } System.out.println("Elements inside the brackets: "+myStack); myStack.clear(); index1 =0; index2 =0; } private void InsideTheParentheses(){ for(int x=0; x<getCharArray().length;x++){ if(array[x]=='('){ index1 =x; } if(array[x]==')'){ index2 = x; } for(int y=index1+1; y<index2; y++){ myStack.push(array[y]); } } System.out.println("Elements inside the parentheses: "+myStack); myStack.clear(); index1 =0; index2 =0; } private void setCharArray(char [] array){ this.array = array; } private char [] getCharArray(){ return array; } private void setInput(String input){ this.input = input; } private String getInput(){ return input; } public static void main(String args[]){ Brackets b = new Brackets(); } }
anyone can help?tnx



Reply With Quote
