-4

I want to make a registration program. So there is 5 enrollments possbiel, lets say music, English, history, sports, photography. I want to make it so it will ask the user to enter to enroll into only 3 units out of the 5 options listed above. only those 5 are the options, if anything else is entered say like "math" or "jdwhagdwa" then it will ask the user to re enter it. Once the student enters the unit it should tell them the classes they have enrolled into and the fee for each class

import java.util.*;
        
    public class CheckMarkEnroll
       {    
         public void Enroll()
           {    
             Scanner in = new Scanner(System.in);
             Scanner sc = new Scanner(System.in);
         
             int i = 0;
             String studentlogin = "u123";
             //Unit name
             String english="Math";
             String history="History";
             String sports="Sports";
             String photography="Photography";
             String music="Music";             
           
             String musicfee ="$1320";
             String englishfee="$1890";
             String historyfee="$1890";
             String sportsfee="$1600";
             String photographyfee="$1500";
            
             String studentId="";
             String course1="";
             String course2="";
             String course3="";

        
             //============================================START OF 
             PROGRAM==================================================================//
             System.out.println("Welcome to student enrollment");
             System.out.println("Please enter your student ID");
             
             studentId = in.nextLine();
             
             System.out.println("Please enter name of units you would like to enroll into. You may enroll 
             into only 4 units. Upon entering please enter if this is your first time or you are 
             repeating this unit. At the end your fees required will be shown"); 
             System.out.println("List of units available to enroll:");
             System.out.println("music");
             System.out.println("english");
             System.out.println("history");
             System.out.println("sports");
             System.out.println("photography");
             
                               
             System.out.println("Please enter name of unit #1 : ");
             course1= sc.nextLine();
             
             System.out.println("Please enter name of unit #2 : ");
             course2= sc.nextLine();
             
             System.out.println("Please enter name of unit #3 : ");
             course3= sc.nextLine();
             
             
             
             
             
            }
        }
             
             
             
Ryan M
  • 18,333
  • 31
  • 67
  • 74
  • so you are asking how to compare Strings? – Scary Wombat Oct 21 '20 at 05:50
  • Sure I guess, im new to java so wouldn't know. How would I go about this? – SkinnyFatGuy Oct 21 '20 at 06:02
  • 1
    The answer posted below is a good solution but on the topic of comparing strings in Java: Always compare two strings for content in Java with equals() method and never with == (https://stackoverflow.com/a/513839/13490121) –  Oct 21 '20 at 06:09
  • 2
    Note: this place isnt programming school, so you got an answer that uses various more advanced concepts ... which for sure is overburdening you right now. Meaning: this is not a good place to *learn* Java. We help you with specific problems, but people answering here aren't teachers who will adapt their input to your skill level. So: you better pick up a good book, and work through that in your own pace. Or talk a lot to your peers in your class, or your tutors. But don't assume that this place is a good "learning buddy" for you. This is not meant to scare you away, just to set expectations. – GhostCat Oct 21 '20 at 06:15
  • 3
    Please don't make more work for others by vandalizing your posts. By posting on the Stack Exchange (SE) network, you've granted a non-revocable right, under the [CC BY-SA license](https://creativecommons.org/licenses/by-sa/4.0), for SE to distribute the content (regardless of your future choices). By SE policy, the non-vandalized version is distributed. Thus, any such destructive edits will be reverted. Please see [How does deleting work?](https://meta.stackexchange.com/q/5221) for more information on how deleting content works on this site. – Ryan M Oct 21 '20 at 07:10

1 Answers1

1

You could work with Sets (or any Collection). One containing all your possible courses and one that saves your entered courses.

Set<String> courses = new HashSet<>();
Set<String> possibleCourses = new HashSet<>();
possibleCourses.add(english);
possibleCourses.add(history);
possibleCourses.add(sports);
possibleCourses.add(photography);
possibleCourses.add(music);

System.out.println("List of units available to enroll: " + possibleCourses);

while (courses.size() < 3) {
    System.out.println("Please enter name of unit #" + (courses.size() + 1) + " : ");
    String course = sc.nextLine();
    if (!possibleCourses.contains(course)) {
        System.out.println("Please enter a valid course name! Your options are: " + possibleCourses);
        continue;
    }
    if (!courses.contains(course)) {
        courses.add(course);
    } else {
        System.out.println("You have already taken course: " + course);
    }
}

System.out.println("You have taken the courses: " + courses);
Milgo
  • 2,617
  • 4
  • 22
  • 37
  • 2
    @SkinnyFatGuy Please read the comment I just gave you. You are exactly going down the wrong path. this place isnt intended for "but now that i took your answer, I am stuck somewhere else". One question, and then answers to that. This is not a free tutoring service were people are likely to sit down with you and work through your whole assignment with you. You have to find other places if you need that. – GhostCat Oct 21 '20 at 06:19