0

When I run the code I do not get my desired result for the setDepartment method. I'm trying to trap the user if they entered a wrong selection. When I execute the code and put in a value (string) that should without a doubt execute, it does not execute. I have tested to see where the problem could lay. The only thing that worked is when I tried hardcoding the correct value into the method. If I put in an incorrect value and trap the user, and put in the correct value, the user is still trapped in the loop. I checked to see if there are any logical errors in my loop, but I could not find a problem. I am at a complete loss, I can't wait to find what I messed up on.

Please ignore the courseCost instance variable I haven't worked on that yet, and please disregard the system.out.println(department) after the department = department.toUpperCase in the setDepartment method. I was using the system.out.println(department) in the setDepartment method to rule out any possible errors that may have been on the department = department.toUpperCase line of code. Also, you can ignore everything else, everything else works just fine. Thank you for taking the time to take a look.

import java.util.Scanner;

public class Course {
    static Scanner keyboard = new Scanner(System.in);
    private String department;
    private int courseNumber;
    private int courseCredits;
    private double courseCost;

    public Course () {
        department = "unknown";
        courseNumber = 0;
        courseCost = 0;
        courseCredits = 0;
    }

    public Course(String department, int courseNumber, int courseCredits) {
        setDepartment(department);
        setCourseNumber(courseNumber);
        setCourseCredits(courseCredits);
        //courseCost = no value will be passed to the constructor, courseCost will be calculated as courseCredits/2 * $500, see setter below (if the course is a Lab Course add $100 to the cost)
    }

    public String getDepartment() {
        return department;
    }

    public int getCourseNumber() {
        return courseNumber;
    }

    public int getCourseCredits() {
        return courseCredits;
    }

    public double getCourseCost() {
        return courseCost;
    }

    public void setDepartment(String department) {

        boolean enteredCorrectly = false;
        //department = department.toUpperCase();

        do{
            department = department.toUpperCase();
            System.out.println(department);
            //ENGL, MATH, COMP, HIST, HUMN, SCIE, LANG, PHYS
            if( (department == "ENGL") || (department == "MATH") || (department == "COMP") || 
                (department == "HIST") || (department == "HUMN") || (department == "SCIE") || 
                (department == "LANG") || (department == "PHYS") ) {
                this.department = department;
                enteredCorrectly = true;
            } else {
                System.out.println("Please re-enter a valid department.");
                department = keyboard.nextLine();

            }
        } while(!(enteredCorrectly));

    }

    public void setCourseNumber(int courseNumber) {

        boolean enterCorrectly = false;

        do{
            if(1 <= courseNumber && courseNumber <= 399) {
                this.courseNumber = courseNumber;
                enterCorrectly = true;
            } else {
                System.out.println("Please re-enter a valid course number.");
                courseNumber = keyboard.nextInt();
                keyboard.nextLine();
            }
        } while(!(enterCorrectly));

    }

    public void setCourseCredits(int courseCredits) {

        boolean enterCorrectly = false;

        do{
            if(courseCredits == 3 || courseCredits == 4 || courseCredits == 6) {
                this.courseCredits = courseCredits;
                enterCorrectly = true;
            } else {
                System.out.println("Please re-enter a valid course credits.");
                courseCredits = keyboard.nextInt();
                keyboard.nextLine();
            }
        } while(!(enterCorrectly));

    }

    public void setCourseCost(double courseCost) {
        this.courseCost = courseCost;
    }

    public static void main(String[] args) {
        Course c = new Course ("engl", 991, 1);
    }
}

2 Answers2

1

I think the reason it is not giving you the expected results is because in the comparison block you are using the operator ==, which means you are comparing the reference of the strings:

if( (department == "ENGL") || (department == "MATH") || (department == "COMP") || 
    (department == "HIST") || (department == "HUMN") || (department == "SCIE") || 
    (department == "LANG") || (department == "PHYS") ) {
    this.department = department;
    enteredCorrectly = true;
}

In Java, to compare the value of strings, you should use the .equals() method:

if( (department.equals("ENGL") || (department.equals("MATH") || (department.equals("COMP") || 
    (department.equals("HIST") || (department.equals("HUMN") || (department.equals("SCIE") || 
    (department.equals("LANG") || (department.equals("PHYS") ) {
    this.department = department;
    enteredCorrectly = true;
}

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
foo0x29a
  • 806
  • 7
  • 6
  • If that's the case then the question is a [duplicate](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) and should be marked as such, not answered. – Federico klez Culloca Mar 08 '20 at 11:14
  • How would a beginner have known to look for a question on string equality? – NomadMaker Mar 08 '20 at 11:30
  • I should kiss both of you Federico & @Thiago.. *muah* *muah* (no homo) Man, you guys don't know how long I've been stuck on this, thank you very much! – chillieguy7 Mar 08 '20 at 12:06
  • You too @NomadMaker, thank you! I agree with you, perhaps they could create a bot that allows questions to remain up until the noob (me) sees it and then they can take it down. I think that would be an awesome feature. – chillieguy7 Mar 08 '20 at 12:19
  • @FedericoklezCulloca Sorry about that. I am getting familiar with answering questions. Could you please tell me how I should proceed? – foo0x29a Mar 08 '20 at 12:52
  • @ThiagoNobayashi don't worry, I was not reprimanding you, just using this as a teaching opportunity :) – Federico klez Culloca Mar 08 '20 at 18:23
  • @NomadMaker nothing in particular, I was just pointing out that there's another answer answering the same basic question. It's not like I'm bashing Thiago or OP :) – Federico klez Culloca Mar 08 '20 at 18:26
0

the probable reason is in two thing - declaring a private global variable:

private String department;

and these lines:

department = department.toUpperCase();
System.out.println(department);

To my mind, the problem is in same names between global private var and method param. Just try to rename the method parameter to departmentParam (for example) and then try to print it.

Villa_7
  • 508
  • 4
  • 14
  • I'm not so sure. Later on they do `this.department = department` so apparently they know what they're doing in that fragment. The only despicable thing is the choice of the name, but otherwise it should work as intended. – Federico klez Culloca Mar 08 '20 at 10:53
  • Thank you for the help @Villa, it's very much appreciated. This wasn't the problem, however, the problem was we can't use "==" to compare strings, the preferred way is .equals method! – chillieguy7 Mar 08 '20 at 12:10