Question: My code right now doesn't accept special Regular Expression and I'm having a hard time implementing it to my code. The only thing that works

My code right now doesn't accept special Regular Expression and I'm having a hard time implementing it to my code. The only thing that works right now is when I do this test: "//; 1;2;3" But when I do this "//$ 1$2$3" does not work. Here's my test file: import static org.junit.Assert.assertEquals;

import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException;

public class TestingCalculate {

@Test public void emptyString() { assertEquals(Calculator.Add(""),0); } @Test public void sumOfNumbersDelimtedByComma() { assertEquals(Calculator.Add("1,2,3"),6); } @Test public void sumOfNumbersDelimetedByNewLine() { assertEquals(Calculator.Add("1 3,3"),7); } @Test public void sumOfNumbersDelimetedByUserChoice() { assertEquals(Calculator.Add("//; 1;2;2"),5); } } Here's my calculator file

import java.util.Arrays; import java.util.stream.IntStream;

public class Calculator { private String delimeter; private String digits; private Calculator(String delimeter, String digits) { this.delimeter = delimeter; this.digits = digits; } private int sum() { if(getNum().anyMatch(n -> n <0)) { throw new IllegalArgumentException(); } return getNum().sum(); }

private IntStream getNum() { return Arrays.stream(digits.split(delimeter)).mapToInt(Integer::parseInt); } public static int Add(String numbers) { if(numbers.isEmpty()) { return 0; } Calculator calculator = parseInput(numbers); return calculator.sum(); }

private static Calculator parseInput(String numbers) { if(numbers.startsWith("//")) { String [] parts = numbers.split(" ",2); return new Calculator(parts[0].substring(2),parts[1]); }else { return new Calculator(",| ", numbers); } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!