Skip to content

Instantly share code, notes, and snippets.

Embed
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: silvestri@stcc.edu
* 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 = matcher.group(1);
int number = Integer.parseInt(matcher.group(2));
boolean isLab = matcher.group(3) != 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) {
System.out.print(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(System.in);
do {
process(sc, args);
} while (doThisAgain(sc, CONTINUE_PROMPT));
sc.close();
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.