Code

Regular Expression Java – Mock Test Answers

The following string (inputs) is used to simulate a clients answers to five questions. The format is the question number followed by a delimiter and the alphabetic choice for that question.

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class CoreJavaApplicationTests {
 final String inputs = "1:A, 2:B, 3:B, 4:A, 5:C";
 final String pattern = "[0-9]:[a-zA-Z],*";

 @Test
 public void parseAllSolution() {
 Matcher matchAll = Pattern.compile(pattern).matcher(inputs);
 printSections(matchAll);
 }

 private void printSections(Matcher matcher) {
 while (matcher.find()) {
 String item = matcher.group();

 Integer id = Integer.valueOf(item.replaceAll(patternSuffix,"").replaceAll(",",""));
 System.out.printf("\n PREFIX %s%n", id);

 String meta = item.replaceAll(patternPrefix,"").replaceAll(",","");
 System.out.printf("SUFFIX %s%n", meta);

 disp(id,meta);
 }

 }

 private void disp(Integer question, String response) {
 System.out.printf("ID: %d, RESP: %s%n", question, response);

 baselist.forEach(item -> {
 if (item.getId() == question && item.getResponse().equalsIgnoreCase(response)) {
 System.out.printf("Choice: Question - %d, Answer - %s, Data - %s%n", question, item.getResponse(), item.getDescriptor());
 }
 });
 }
}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s