-4

I am trying to code a program that has conditions and asks these questions for 3 courses:

  1. what is the name of the course?
  2. How many students are enrolled in the course?
  3. What is student capacity for this course?
  4. When is the final exam date for the third course?

The condition is the final exam date for course 2 can't be the same as final exam date for course 1 and final exam date for course 3 can't be the same as course 1 or course 2. It checks for course one but terminates before checking for course 2.

Here is the code:

import java.util.Scanner;

public class CourseTest2 {

    public static void main(String[] args) {
        Scanner scanoption = new Scanner(System.in);
        Scanner in = new Scanner(System. in);
        String name1;
        int enroll1;
        int cap1;
        String final1;
        String answer;


        //This is for the first course
        System.out.println("What is the name of the course?");
        name1 = in. nextLine();
        System.out.println("How many students are enrolled in the course?");
        enroll1 = in. nextInt();
        System.out.println("What is student capacity for this course?");
        cap1 = in. nextInt(); in.nextLine();
    while (enroll1 > cap1)
    {
        System.out.println("Amount of Enrolled Students cannot exceed Student Capacity.Press Enter");
        in.nextLine();
        System.out.println("How many students are enrolled in the course?");
        enroll1 = in. nextInt();
        System.out.println("What is student capacity for this course?");
        cap1 = in. nextInt(); in.nextLine();
    }
        System.out.println("When is the final exam date for this course?");
        final1 = in. nextLine();
        Course course1 = new Course (name1);
        System.out.println("Do you want to add another course? (Y/N)"); // this is to repeat the questions so the use can enter information for the next class obejct
        answer = in.nextLine();
        course1.setName(name1);
        course1.setEnrolledStudentNum(enroll1);
        course1.setFinalExamDate(final1);

    //2nd Course Object
    String name2;
    int enroll2;
    int cap2;
    String final2;
    switch (answer) 
    {

        case "Y":
        case "y": System.out.println("What is the name of the second course?");
                name2 = in. nextLine();
                System.out.println("How many students are enrolled in the course?");
                enroll2 = in. nextInt();
                System.out.println("What is student capacity for this course?");
                cap2 = in. nextInt();in.nextLine();
                while (enroll2 > cap2)
                {
                    System.out.println("Amount of Enrolled Students cannot exceed Student Capacity.Press Enter");
                    in.nextLine();
                    System.out.println("How many students are enrolled in the course?");
                    enroll2 = in. nextInt();
                    System.out.println("What is student capacity for this course?");
                    cap2 = in. nextInt(); in.nextLine();
                }
                do 
                {
                System.out.println("When is the final exam date for this course?");
                final2 = in. nextLine();
                }
                while ( final2 == final1);
                {
                    System.out.println("Final Exam Date for Second Course cannot be the same as the first course. Press enter and try again");
                    in.nextLine();
                    System.out.println("When is the final exam date for this course?");
                    final2 = in. nextLine();
                 }

                Course course2 = new Course (name2);
                System.out.println("Do you want to add another course? (Y/N)");// this is to repeat the questions so the use can enter information for the next class obejct
                answer = in.nextLine();
                course2.setName(name2);
                course2.setEnrolledStudentNum(enroll2);
                course2.setFinalExamDate(final2);
                 break;

        case "N":
        case "n":System.exit(0);
             break;

        default :
             System.out.println("invalid choice")   ;
             break;
    }
    //3rd Class Object

        String final3 = null;

    switch (answer)
    {
        case "Y":
        case "y":       System.out.println("What is the name of the third course?");
        String name3 = in. nextLine();
        System.out.println("How many students are enrolled in the course?");
        int enroll3 = in. nextInt();
        System.out.println("What is student capacity for this course?");
        int cap3 = in. nextInt();in.nextLine();
        while (enroll3 > cap3)
        {
            System.out.println("Amount of Enrolled Students cannot exceed Student Capacity. Press Enter");
            in.nextLine();
            System.out.println("How many students are enrolled in the course?");
            enroll3 = in. nextInt();
            System.out.println("What is student capacity for this course?");
            cap3 = in. nextInt(); in.nextLine();
        }
        do 
        {
        System.out.println("When is the final exam date for the third course?");
        final3 = in. nextLine();
        }
        while ( final3 == final1 && final3 == final2);
        {
            System.out.println("Final Exam Date for third Course cannot be the same as the first or second course. Press enter and try again");
            in.nextLine();
            System.out.println("When is the final exam date for this course?");
            final3 = in. nextLine();
            in.nextLine();
         }
        Course course3 = new Course (name3);
        course3.setName(name3);
        course3.setEnrolledStudentNum(enroll3);
        course3.setFinalExamDate(final3); `

This is what it prints out:

What is the name of the course?
a
How many students are enrolled in the course?
5
What is student capacity for this course?
20
When is the final exam date for this course?
05/05
Do you want to add another course? (Y/N)
y
What is the name of the second course?
b
How many students are enrolled in the course?
10
What is student capacity for this course?
5
Amount of Enrolled Students cannot exceed Student Capacity.Press Enter

How many students are enrolled in the course?
10
What is student capacity for this course?
20
When is the final exam date for this course?
05/05
Final Exam Date for Second Course cannot be the same as the first course. Press enter and try again

When is the final exam date for this course?
10/10
Do you want to add another course? (Y/N)
y
What is the name of the third course?
c
How many students are enrolled in the course?
15
What is student capacity for this course?
10
Amount of Enrolled Students cannot exceed Student Capacity. Press Enter

How many students are enrolled in the course?
15
What is student capacity for this course?
20
When is the final exam date for the third course?
05/05
Final Exam Date for third Course cannot be the same as the first or second course. Press enter and try again

When is the final exam date for this course?
10/10

As you can see the second attempt to change the final exam date meets the condition so it should say "Final Exam Date for third Course cannot be the same as the first or second course. Press enter and try again" but it just terminates after the user presses enter

khelwood
  • 55,782
  • 14
  • 81
  • 108
  • 1
    Classic student mistake: too much code, poor decomposition. Write small methods that you can test by passing data to them. Don't Repeat Yourself - DRY principle. Keep interactions with users completely separate. – duffymo Feb 25 '19 at 15:04
  • 2
    Hi. Now is a good time to learn how to debug. If you are not using an IDE (Integrated Development Environment) such as Eclipse, you should . [Here](https://www.vogella.com/tutorials/EclipseDebugging/article.html) is a tutorial. – OldProgrammer Feb 25 '19 at 15:04
  • Also have a look at https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Mark Feb 25 '19 at 15:07
  • I am using eclipse. Also, the questions are supposed to repeat for the second and third course – Idara Abasi Feb 25 '19 at 15:08
  • I tried the suggestions in the link you gave me @Mark but that just made course 2 not work whereas before it worked i was just course 3 that wasn't working – Idara Abasi Feb 25 '19 at 15:14
  • @IdaraAbasi oh if that had been an answer i wouldn't have posted it as a comment ;) that being said using equals should `not` stop course 2 from working and you should definitely get used to using equals instead of == for comparing strings as == has the nasty habit of only working sometimes. – Mark Feb 25 '19 at 15:19
  • @Mark i am new to java – Idara Abasi Feb 25 '19 at 15:22
  • @Idara Abasi knowing to use .equals vs == for strings is probably something a lot of people don't know, especially students. In most cases == will work as you intend, but it is safer to use .equals – Chris K Feb 25 '19 at 15:29

4 Answers4

1

One issue here:

while ( final3 == final1 && final3 == final2);

Should be

while ( final3 == final1 || final3 == final2);

The condition should be OR not AND.

That being said, I assume this is for a class you are taking. I will give you some advice. This could all be done in a much simpler and cleaner manner with the knowledge you have already demonstrated in the code above. You just need to put more thought into how you can use loops and classes more efficiently to achieve your goal.

Chris K
  • 1,581
  • 1
  • 10
  • 17
1

This is because Java's short circuit evaluation. Check the related answer here

Janekx
  • 631
  • 6
  • 21
0

I would recommend that you have a class Course.

public class Course {
    private String name;
    private int capacity;
    private int enrollment;
    private Date finalExamDate;

    // You add the rest.
}

You might add a CourseCatalog that keeps a List<Course> of Course instances.

It's not too soon to learn about JUnit. Keep your classes and test code separate.

duffymo
  • 305,152
  • 44
  • 369
  • 561
0

Follow this debugging documentation in eclipse here.

Step 1 Add breakpoints in your code.

Step 2 Debug as java app and check one by one

  • the code works perfectly for course 1 and course 2, when i do it for course three after the a second wrong attempt at the final exam date it terminates – Idara Abasi Feb 25 '19 at 15:21
  • did you followed debugging link? or do you know how to add breakpoints ? –  Feb 25 '19 at 15:24
  • this is my first time using eclipse so I couldn't understand it – Idara Abasi Feb 25 '19 at 19:00
  • i would like an explanation for the downvotes please, also any advise on how to condense could would be appreciated as well – Idara Abasi Feb 25 '19 at 19:41
  • this is your first question in stackoverflow so dont worry. Read the guildlines [link](https://stackoverflow.com/help/how-to-ask) –  Feb 25 '19 at 19:43
  • In short give proper explanation , arrange your code properly, give the description so other can reproduce the same problem, –  Feb 25 '19 at 19:51
  • have you found solution? –  Mar 05 '19 at 08:44
  • Also, I don't know if this will help in for my future questions but this class is the first time I've taken Java and coded. – Idara Abasi Apr 19 '19 at 00:18