Reputation: 95
I'm currently working on a project for class but struggling with a bit of coding relating to
ArrayList
s. It is as yet unfinished code; however, when I'm working on the enrollstudent
method I'm having an issue comparing the length of an ArrayList
to the variable amountstudents
.
Below is a copy of the code for the full class. There is another seperate class related but I don't think it's relevant here.
Any help would be greatly appreciated.
import java.util.*;
import java.util.Scanner;
public class Course {
int amountstudents;
String coursename;
String level;
ArrayList<String> students = new ArrayList<String>();
String tutor;
Scanner in = new Scanner(System.in);
public Course(int MaxCapacity) {
MaxCapacity = amountstudents;
tutor = "Not set yet";
coursename = "Not set yet";
level = "Not set yet";
}
public void enrollstudent(String addstudent) {
if(students.size > amountstudents) {
System.out.println("Unfortunately the class is already full so you can not be enrolled at this time");
}
else {
students.add(Student.fname);
}
}
public void courselevel() {
System.out.println("Please enter course level");
level=in.next();
}
public void coursetitle() {
}
}
Upvotes: 1
Views: 2098
Reputation: 28598
You want to use students.size()
. size
is a method on List types, not a property (like length
is on arrays).
Also, in the constructor, you have this reversed:
MaxCapacity = amountstudents;
it should be:
amountstudents = MaxCapacity;
Upvotes: 1
Reputation: 236004
It's students.size()
, since students
is an ArrayList
Upvotes: 1
Reputation: 22830
It's students.size()
, not students.size
.
Also, there's another error:
It should be students.add(addstudent)
, not students.add(Student.fname)
.
Upvotes: 3