1

I am attempting to make a course registration system and one of my classes (Course) is centered around course attributes (ie. Course number, course name, instructors, students). I am making an ArrayList so that the Administrator (one of the user types) may add as many instructors to the course as he/she would like- I have created a Scanner and a String variable and everything, but when I write the .add command, Eclipse highlights ".add" and says "the method .add() is undefined for the type of scanner". Now, I can understand this, but I have no idea how to fix it and I've tried so many ideas.

Here is the method:`

public static String Instructor(){
        String courseInstructors;

        System.out.println("Please add name(s) of course instructors.");
        ArrayList<String> Instructors= new ArrayList<String>();
        Scanner courseInst = new Scanner(System.in);
        courseInstructors = courseInst.next();
        //courseInst.add(courseInstructors); 

        for(String courseInstructors1 : Instructors) {
            courseInstructors1 = courseInstructors;
            courseInst.add(courseInstructors1);
        }

        return;
    }`
2
  • I think you meant to call add on Instructors, not courseInst. Either way, you should name the reference instructors, not Instructors. Commented Feb 2, 2015 at 16:44
  • The for(String courseInstructors1 : Instructors) doesn't make sense to me. Since you just created the Instructors collection, it is empty and there will be no iterations. What is the purpose of this loop? What is it supposed to do? Commented Feb 2, 2015 at 17:01

2 Answers 2

3

Please adhere to Java naming conventions ad use lower case for variable names - instructors instead of Instructors.

Also, you want to add to your arraylist, so call add() on

instructors.add(courseInstructors1)

You may also want to consider choosing better variable naming than courseInstructors1, for instance just courseInstructor, since you are referring to on instructor of all instructors.

Also in your for loop you are doing the following

for(String courseInstructors1 : Instructors) {
    courseInstructors1 = courseInstructors;
    courseInst.add(courseInstructors1);
}

This can be simplified to

for(String courseInstructors1 : Instructors) {
    courseInst.add(courseInstructors);
}

And if you look at the simplification you will see that iterating through Instructors make no sense here, since you are not using the contents of courseInstructors1.

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

2 Comments

I named the variable courseInstructors1 because i needed to define a new String in the for loop (i dont know why). It was not allowing me just use courseInstructors, so i just set them equal to eachother to get around it. annoying, but its all I could think of. Do you know of any other ways to get around that?
Thank you so much. I'm obviously fairly new to Java, so this WOULD be an effective way to go about this, right? (with the for loop) I just want the user to be able to enter multiple instructors for the course.
1

I'm trying to understand what your loop is for.

if you are trying to get multiple instructor names from one input then you need something like this.

//get input
//"John Peggy Adam blah blah"
courseInstructors = courseInst.next();

//split the string by white space
String[] instArr = courseInstructors.split(" ");
//will give array of John, Peggy, Adam, blah, blah

Then do your foreach loop to add them to the list.

for(String inst: instArr){
    instructors.add(inst);
}

Otherwise I would suggest doing something like this so you don't have to worry about splitting names and such.

courseInstructor = courseInst.nextLine();

while(!courseInstructor.equals("done"){
    //add name to list of instructors.
    instructors.add(courseInstructor);
    //get next name.
    courseInstructor = courseInt.nextLin();
    //if the user types done, it will break the loop.
    //otherwise come back around and add it and get next input.
}

3 Comments

the function of my for loop is to allow the user to enter multiple names. For example, the user would add "Paul" then click enter; he would then add "James" click enter and it would add to the arraylist over and over again.
That is what i figured you wanted but I wasn't sure. Then what you would need is what i posted at the bottom.
something like it anyways.

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.