Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Recognizing STCC Course Numbers Using RegEx
* Name: Prof. Antonio C. Silvestri
* Date: 02/24/2020
* Course Number: CSC-112
* Course Name: Intermediate Java Programming
* Problem Number: NA
* Email:
* Recognizing STCC Course Numbers
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RecognizeCourseNumbers {
private final static String TITLE = "Recognizing STCC Course Numbers V1.0";
private final static String CONTINUE_PROMPT = "Do this again? [y/N] ";
// Put as many methods you need here
private static Object[] recognizeCourseNo(String courseNo) {
final String regex = "^\\s*([A-Za-z]{3})-?(\\d{3})([Ll])?\\s*$";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(courseNo);
if (matcher.find()) {
String dept =;
int number = Integer.parseInt(;
boolean isLab = != null;
return new Object[] {dept, number, isLab};
return null;
// Start your logic coding in the process method
private static void process(Scanner sc, String args[]) {
System.out.print("Enter Course Number: ");
String courseNo = sc.nextLine();
Object[] courseDetails = recognizeCourseNo(courseNo);
if (courseDetails == null) {
System.out.printf("%s is not a valid STCC Course No.\n", courseNo);
else {
System.out.printf("Dept: %s\nNumber: %d\nIs Lab: %b\n", courseDetails[0], courseDetails[1], courseDetails[2]);
// Do not change the doThisAgain method
private static boolean doThisAgain(Scanner sc, String prompt) {
String doOver = sc.nextLine();
return doOver.trim().equalsIgnoreCase("Y");
// Do not change the main method
public static void main(String args[]) {
System.out.println("Welcome to " + TITLE);
Scanner sc = new Scanner(;
do {
process(sc, args);
} while (doThisAgain(sc, CONTINUE_PROMPT));
System.out.println("Thank you for using " + TITLE);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.