Skip to content

Finished lab #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Finished part 1 and 2
  • Loading branch information
AnthonyJordan committed Mar 12, 2018
commit 29b17274e15d88f311657bf02c97a96800b2425f
101 changes: 101 additions & 0 deletions src/main/java/io/zipcoder/ParenChecker.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,105 @@
package io.zipcoder;

import com.sun.xml.internal.fastinfoset.util.CharArray;

import java.util.Stack;

public class ParenChecker {
private Stack<Character> stackOfChars = new Stack();

public boolean parenChecker(String inputString) {
char[] arrayAsChar = inputString.toCharArray();
for (int i = 0; i < arrayAsChar.length; i++) {
characterChecker(arrayAsChar[i]);
}

if (stackOfChars.isEmpty()) {
return true;
} else {
return false;
}
}

private void characterChecker(char inputChar) {
switch (inputChar) {
case '(':
stackOfChars.push(inputChar);
break;
case '{':
stackOfChars.push(inputChar);
break;
case '[':
stackOfChars.push(inputChar);
break;
case '<':
stackOfChars.push(inputChar);
break;
case ')':
if (!stackOfChars.empty()) {
if (stackOfChars.peek().equals('(')) {
stackOfChars.pop();
} else {
stackOfChars.push(inputChar);
}
} else {
stackOfChars.push(inputChar);
}
break;
case '}':
if (!stackOfChars.empty()) {
if (stackOfChars.peek().equals('{')) {
stackOfChars.pop();
} else {
stackOfChars.push(inputChar);
}
} else {
stackOfChars.push(inputChar);
}
break;
case ']':
if (!stackOfChars.empty()) {
if (stackOfChars.peek().equals('[')) {
stackOfChars.pop();
} else {
stackOfChars.push(inputChar);
}
} else {
stackOfChars.push(inputChar);
}
break;
case '>':
if (!stackOfChars.empty()) {
if (stackOfChars.peek().equals('<')) {
stackOfChars.pop();
} else {
stackOfChars.push(inputChar);
}
} else {
stackOfChars.push(inputChar);
}
break;
case '\"':
if (!stackOfChars.empty()) {
if (stackOfChars.peek().equals('\"')) {
stackOfChars.pop();
} else {
stackOfChars.push(inputChar);
}
} else {
stackOfChars.push(inputChar);
}
break;
case '\'':
if (!stackOfChars.empty()) {
if (stackOfChars.peek().equals('\'')) {
stackOfChars.pop();
} else {
stackOfChars.push(inputChar);
}
} else {
stackOfChars.push(inputChar);
}
break;
}
}
}
33 changes: 32 additions & 1 deletion src/test/java/io/zipcoder/ParenCheckerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,37 @@
import org.junit.Assert;
import org.junit.Test;

import javax.xml.ws.RequestWrapper;

public class ParenCheckerTest {

}
@Test
public void parenCheckerTest1(){
ParenChecker testChecker = new ParenChecker();
boolean actual = testChecker.parenChecker("()()<>\"\"[]{}\'\'");
Assert.assertTrue(actual);
}

@Test
public void parenCheckerTest2(){
ParenChecker testChecker = new ParenChecker();
boolean actual = testChecker.parenChecker("ufeuifbbifeiu><<()");
Assert.assertFalse(actual);
}

@Test
public void parenCheckerTest3(){
ParenChecker testChecker = new ParenChecker();
boolean actual = testChecker.parenChecker("\"(({}))\"");
Assert.assertTrue(actual);
}

@Test
public void parenCheckerTest4(){
ParenChecker testChecker = new ParenChecker();
boolean actual = testChecker.parenChecker("fsf(kfbf{}jnje)<>\"dsdjhsjdhj\"");
Assert.assertTrue(actual);
}

}