1

I have an array of objects, and i'm looking to check if a variable in the object has a particular name. Eventually id like to do it for every object in the array, but i only was testing it on the first index. Im not sure if an arraylist would be better for this. (I have separate faculty/classroom/course/textbook/name classes)

public static void startCourse(){
    Course[] course = new Course[4];
    Scanner input = new Scanner(System.in);
    for(int i = 0;i < course.length;i++){
        System.out.println("Enter Course Number and course title: ");
        String courseNumber = input.nextLine();
        String courseTitle = input.nextLine();
        course[i] = new Course(courseNumber,courseTitle);
        //FACULTY
        System.out.println("Faculty: /nEnter First Name: ");
        String facultyfName = input.nextLine();
        System.out.println("Enter Last Name: ");
        String facultylName = input.nextLine();


        course[i].setFaculty(new Faculty(facultyfName,facultylName));//doesnt set name in constructor???

        course[i].getFaculty().getName().setfName(facultyfName);
        course[i].getFaculty().getName().setlName(facultylName);

        course[i].setTextbook(new Textbook("Intro to java","123456",59.99));
        course[i].setClassroom(new Classroom("R540",26,true));

        Student student1 = new Student("Yulissa","Lucero");
        Student student2 = new Student("Aaron","Folborg");

        Student[] students = {student1,student2};
        //input.close();
        System.out.println(course[i]);
        //System.out.println(students);
    }
    System.out.println(course[0].getFaculty().getName().equals("Ben"));
}
3
  • 1
    So what is your question? Is your code working as intended? If not please provide your expected result and the actual result along with any error messages or exceptions. Commented Sep 24, 2017 at 3:42
  • I should get a return of true if i have "Ben" typed in that particular index, but im getting false everytime. Commented Sep 24, 2017 at 3:43
  • Well, did you give the exact string "Ben" as the first name of the faculty when you entered data for the first course? It wouldn't match if you put anything other than exactly "Ben", such as "BEN" or " Ben" (with spaces in front) or "Ben " (with spaces after). Commented Sep 24, 2017 at 3:53

2 Answers 2

1

There is confusion seeing your code, when you are setting Faculty name you are using below statements :

course[i].getFaculty().getName().setfName(facultyfName);
course[i].getFaculty().getName().setlName(facultylName);

Which made be believe that getName() would be returning some kind of object having two properties fname and lname But when you are comparing the faculty name, you are using below statement :

course[0].getFaculty().getName().equals("Ben")

How can you compare Object with String, you should either use something like getfName() or getlName().

Sign up to request clarification or add additional context in comments.

2 Comments

face palm that was all i was missing thank you. may i ask Is this a good approach using array or would arraylist be better for this?
If you are sure with the numbers then you can opt for array else you should go with ArrayList, Internally ArrayList also uses arrays so in both cases your logic is going to be index based.
0

In context of code :

course[i].setFaculty(new Faculty(facultyfName,facultylName));//doesnt set name in constructor???

should be valid when Course class has a setter for Faculty faculty attribute and Faculty class has a constructor for attributes of Name name field within it as:

Faculty(String fname, String lname) {
   this.name.setfName(fname);
   this.name.setLname(lname);
}

The comparison should hold true when the first input provided to

facultyfName = input.nextLine();

is "Ben" and then comparing the first name of the faculty using :

System.out.println(course[0].getFaculty().getName().getfName().equals("Ben"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.