fork download
  1. import java.util.*;
  2. import java.lang.*;
  3.  
  4. class Main
  5. {
  6. public static void main (String[] args) throws java.lang.Exception
  7. {
  8. char [] openParenthesis = {'(','{','['};
  9. char [] closeParenthesis = {')','}',']'};
  10. String testString = "{}()[] ";
  11. ArrayList<Character> characterList = new ArrayList<Character>();
  12. for(char c : testString.toCharArray())
  13. characterList.add(c);
  14.  
  15. for(int i = 0; i < characterList.size()-1; i++) {
  16. System.out.println("1st Loop: " + characterList.get(i));
  17. System.out.println("1st Loop: " + characterList.get(i + 1));
  18. System.out.println("1st Loop: " + i);
  19.  
  20. for(int j = 0; j < openParenthesis.length; j++) {
  21. if (characterList.get(i) == openParenthesis[j]) {
  22.  
  23. if(characterList.get(i + 1) == closeParenthesis[j]) {
  24. System.out.println("********* Nested Match");
  25. System.out.println(characterList.get(i));
  26. System.out.println(characterList.get(i + 1));
  27. System.out.println();
  28. characterList.remove(i + 1);
  29. characterList.remove(i);
  30. i = -1;
  31. break;
  32. }
  33. }
  34. }
  35. }
  36.  
  37. }
  38. }
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
1st Loop: {
1st Loop: }
1st Loop: 0
********* Nested Match
{
}

1st Loop: (
1st Loop: )
1st Loop: 0
********* Nested Match
(
)

1st Loop: [
1st Loop: ]
1st Loop: 0
********* Nested Match
[
]