0

I am working on implementing code using MVC for my class however I am running into an error I cant seem to figure out. The only parts of the assignment I am supposed to change are Model, View, and Controller so the rest of the classes were already completed for us.

When I run the code I get an error in the controller saying:

"error: incompatible types: Student cannot be converted to String view.DisplayOnButton(model.getInfoForStudent(0), 0);"

How do I fix this?

Here is what I have:

Controller -

public class Controller { Model model; View view; public Controller(Model model, View view) { this.model = model; this.view = view; SetViewFromModel(); } public void SetViewFromModel() { view.DisplayOnButton(model.getInfoForStudent(0), 0); view.DisplayOnButton(model.getInfoForStudent(1), 1); view.DisplayOnButton(model.getInfoForStudent(2), 2); } } 

Model -

 public class Model { ArrayList<Student> sts = new ArrayList<>(); public Model() { //creates 3 students MailAddress addr1 = new MailAddress("107 W College Avenue", "State College", "PA", 16801); Student st1 = new Student("Emily", "Smith", 20, addr1); MailAddress addr2 = new MailAddress("200 W College Avenue", "State College", "PA", 16801); Student st2 = new Student("Mary", "Doe", 20, addr2); MailAddress addr3 = new MailAddress("300 W College Avenue", "State College", "PA", 16801); Student st3 = new Student("John", "Doe", 20, addr3); //add them to the array of students sts.add(st1); sts.add(st2); sts.add(st3); } public Student getInfoForStudent (int studentIndex) { return sts.get(studentIndex); } } 

View -

public void DisplayOnButton(String infoToDisplay, int onButton) { mf.getIp().DisplayOnButton(infoToDisplay, onButton); } 
3
  • 1
    can you put your stacktrace (the error that you get)? there you should have the information about the line that caused the error Commented Oct 6, 2018 at 6:15
  • Call toString() on student objects? But please read minimal reproducible example and enhance your question accordingly. Your question is missing various details we would need to help you. Commented Oct 6, 2018 at 6:17
  • DisplayOnButton has a requirement that the parameter must be a String, either change the parameter type or pass in a String Commented Oct 6, 2018 at 6:25

1 Answer 1

1

your method DisplayOnButton is expecting a String and you are passing an Student, you will have to convert your getInfoForStudent to return a String or convert the Student to String.

I recommend you to overwrite toString() for your mail and student so it creates an String with the information that you expect.

for example:

public class Student { private final String name; private final String surname; private final int age; private final MailAddress mailAddress; public Student(String name, String surname, int age, MailAddress mailAddress) { this.name = name; this.surname = surname; this.age = age; this.mailAddress=mailAddress; } @Override public String toString() { return String.format("name: %s surname:%s age:%d mail address:%s", name, surname, age, mailAddress); } } 

you will have to do something similar for mailAddress, so then your getInfoForStudent looks like that:

 public String getInfoForStudent (int studentIndex) { return sts.get(studentIndex).toString(); } 
Sign up to request clarification or add additional context in comments.

3 Comments

this getInfo() is the toString(), so you can change return sts.get(studentIndex) to return sts.get(studentIndex).getInfo()
The Student class is already completed for us in this assignment and its done in a similar fashion as you have it and returns a String. when I try return sts.get(studentIndex).getInfo() I get an error next to the line of code saying "String cannot be converted to student"
so use this method to get a String with the information of your Students

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.