I'm in the process of creating a text UI for an ArrayList
class, and I'm trying to create the methods in the second class that call the ArrayList
methods. However when I'm trying to call a method that adds an object into the ArrayList
, I'm getting an error.
private void addStudent()
{
labClass.enrollStudent();
}
I'm trying to call the method enrollStudent()
from the labClass
class, but it's saying
method enrollStudent cannot be applied to given types
What exactly do I need to do to call this method? It's something I haven't done before.
Here's the full source code for the same class if it's needed.
import java.util.Scanner;
import java.util.*;
public class TextUI
{
private Scanner myScanner;
private LabClass labClass;
public TextUI()
{
myScanner = new Scanner(System.in);
createLabClass();
}
public void menu()
{
int command = -1;
while(command != 0)
{
displayMenu();
command = getCommand();
execute(command);
}
}
public void displayMenu()
{
System.out.println("Select command: ");
System.out.println(" Display Full Class Details: 1");
System.out.println(" Add Student: 2");
System.out.println(" Find Student by Name: 3");
System.out.println(" Set Instructor: 4");
System.out.println(" Set Room Mumber: 5");
System.out.println(" Set Time and Day: 6");
System.out.println(" Remove Student: 7");
System.out.println(" Get Class Size: 8");
System.out.println(" Terminate Program: 0");
}
private void createLabClass()
{
LabClass labClass = new LabClass();
}
private int getCommand()
{
System.out.print("Enter command: ");
int command = myScanner.nextInt();
return command;
}
private void execute(int command)
{
if(command == 0)
{
System.out.println("Terminating program...");
System.exit(0);
}
else
if(command == 8)
{
System.out.println("The class size is: " + getClassSize());
}
}
private int getClassSize()
{
return labClass.getNumberOfStudents();
}
private void addStudent()
{
labClass.enrollStudent();
}
}
Here's the code for LabClass.enrollStudent
/**
* Add a student to this LabClass.
* @param newStudent the student object to be enrolled
*/
public void enrollStudent(Student newStudent)
{
students.add(newStudent);
}
addStudent code
private void execute(int command)
{
if(command == 0)
{
System.out.println("Terminating program...");
System.exit(0);
}
else if(command == 1)
{
labClass.getLabClass();
}
else if(command == 2)
{
addStudent();
}
}
It looks like you're overwriting your class variable labClass
In your createLabClass method, remove the leading LabClass
. It should look like this
private void createLabClass()
{
labClass = new LabClass();
}
I think what is happening is that you are attempting to access labClass.enrollStudent();
from your class variable, which is never instantiated. Instead, you were instantiating a local version of it that was never used
Then, when it comes to your LabClass implementation, something like this should suffice
public class LabClass() {
private ArrayList<Student> students = new ArrayList<>();
public void enrollStudent(Student newStudent) {
students.add(newStudent);
}
}
Obviously, you may have more in your class, but without knowledge of it, I wrote it as short and simple as possible
So in addition to what @ Brandon & @ Denis said, it turned out that you are calling the enrollStudent method with no parameters. You need to pass in a Student
object to the method. Plus, you need to declare your students' arraylist as a member variable and initialize it :
public class TextUI
{
private List<Student> students = new ArrayList<Student>();
private Scanner myScanner;
private LabClass labClass;
public TextUI()
{
myScanner = new Scanner(System.in);
createLabClass();
}
.......
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.