I’m working on a Java program for student course registration. I wrote the validateChoice
method but I’m facing two issues:
- The program treats 0 as a duplicate instead of an invalid course number.
- It allows two duplicate entries before showing an error message.
The program should not allow any duplicates. Can anyone spot what’s causing these problems? I’ve already submitted the assignment since it works with the given course codes, but I’m curious about fixing these issues for my own understanding.
Here’s a simplified version of the validateChoice
method:
public static int validateChoice(int choice, int first, int second, int third, int credits) {
if (choice < 1 || choice > 7) {
return -1; // Invalid course number
}
if (choice == first || choice == second || choice == third) {
return -2; // Duplicate course
}
if (credits >= 9) {
return -3; // Credit limit exceeded
}
return 0; // Valid choice
}
Any ideas on how to improve this code?
hey oliver, i’ve dealt with similar stuff before. for the 0 issue, just tweak ur condition:
if (choice < 1 || choice > 7) {
return -1; // invalid course num
}
for duplicates, use a List instead:
List courses = new ArrayList<>();
if (courses.contains(choice)) {
return -2; // duplicate
}
courses.add(choice);
This should catch all dupes right away. hope it helps!
I’ve encountered similar issues when working on course registration systems. Here’s what I’ve found helpful:
For the first problem, you need to explicitly check if the choice is 0. Add this condition to your initial if statement:
if (choice <= 0 || choice > 7) {
return -1; // Invalid course number
}
As for the duplicate entries, using individual variables for courses isn’t ideal. Instead, use an array or ArrayList to store selected courses. This way, you can easily check for duplicates:
List selectedCourses = new ArrayList<>();
// …
if (selectedCourses.contains(choice)) {
return -2; // Duplicate course
}
selectedCourses.add(choice);
This approach will catch duplicates immediately and is more scalable if you need to handle more courses in the future. Hope this helps improve your system!
Hey Oliver63! I can totally see why you’re scratching your head over this one.
Course registration systems can be tricky beasts!
For that pesky 0 issue, have you considered tweaking your condition slightly? Maybe something like:
if (choice <= 0 || choice > 7) {
return -1; // Oops, not a valid course number!
}
That should catch those sneaky zeros right off the bat.
Now, about those duplicates… Hmm, have you thought about using a List instead of individual variables? It could make your life way easier! Something like:
List coursePicks = new ArrayList<>();
// Then when checking:
if (coursePicks.contains(choice)) {
return -2; // Uh-oh, you’ve already picked this one!
}
coursePicks.add(choice);
This way, you’re always checking against all the courses picked so far. No more sneaky duplicates slipping through!
What do you think? Does this sound like it might work for you? I’d love to hear if you give it a shot and how it goes! 
Oh, and just curious - what made you interested in creating a course registration system? Are you studying computer science yourself?