0

First of all I am sorry if don't know how to explain my question properly and if the title is not fully correct or not clear enough. This is my first stackoverflow post. I will try to explain my beginner question with example:

I have following Student class:

public class Student {
    private String name;
    private int matrikelnummer;
    public static int STUDENT_COUNT;

    public Student(String name, int matrikelnummer){
        this.name = name;
        this.matrikelnummer = matrikelnummer;
        STUDENT_COUNT++;
    }

    public String toString() {
        return "Student Nr.: " +matrikelnummer+" Name: "+name;
    }
}

and another class Tutorium:

public class Tutorium {
    private int raumNr;
    private String tutor;
    private String fach;
    private Student[] teilnehmer;

    public Tutorium(int kapazitaet, int raumNr, String tutor, String fach) {
        this.raumNr = raumNr;
        this.tutor = tutor;
        this.fach = fach;
        teilnehmer = new Student[kapazitaet];
    }

    public String toString() {
        if (teilnehmer == null) {
            return "Noch keine Teilnehmer";
        }

        String out = "Tutorium " + fach + " Bei " + tutor + " in " + raumNr + " " + "\n";

        return out + --- ; //here i want to print all array element from teilnehmer
    }
} 

For e.g if have following main:

public class TutoriumsVerwaltung {
    public static void main(String[] args) {
        //Studenten und Tutorien erzeugen.
        Student[] studs = {new Student("Tim", 333333),
                new Student("Anna", 444444),
                new Student("Lisa", 345987),
                new Student("Karl", 336292),
                new Student("Elisabeth", 999999)};
        Tutorium[] tuts = {new Tutorium(5, 6057, "Tobias", "PPR-J"),
                new Tutorium(2, 6057, "Roland", "PPR-C"),
                new Tutorium(9, 6051, "Max", "PPR-J")};
        //Infos ueber Tutorien und Studenten ausgeben.
        System.out.println("Es gibt "+Student.STUDENT_COUNT+" Studenten.");
        for (Student student : studs)
            System.out.println(student);
        for (Tutorium tutorium : tuts)
            System.out.println(tutorium);
        System.out.println("");

I want toString() method from Tutorium to print in following format:

Tutorium PPR−J bei Tobias in Raum 6057

Teilnehmer:

Student Nr.: 333333 Name: Tim

Student Nr.: 444444 Name: Anna

Student Nr.: 345987 Name: Lisa

I have tried creating getters/setters (maybe not correctly?) but without success. Would someone care explaining me my problem and solution to it.

Kohei TAMURA
  • 4,970
  • 7
  • 25
  • 49
sam
  • 1
  • 1
  • Yes, problem is not looping. For e.g if I create new Student() with getName and getMatrikelnummer, I get (Null, 0) and not the value given in main. Other case i can't get name and matrikelnummer, it doesn't recognise the variable – sam Jun 25 '17 at 13:40

3 Answers3

0

What you want is to basically get every element from a collection (array in this case). You can easily achieve this by using a for-loop.

Code:

for(Student student : teilnehmer) {

    // If an array isn't full it contains null elements.
    if (student != null) {
        out += student.toString() + "\n"; // '\n' means newline
    }
}
Mibac
  • 8,990
  • 5
  • 33
  • 57
  • Sorry If I am missing very basic thing but now I get following (It doesn't print teilnehmer but only fach, tutor and raumNr, and blank lines below that): Tutorium PPR-J Bei Tobias in 6057 Tutorium PPR-C Bei Roland in 6057 \n Tutorium PPR-J Bei Max in 6051 \n – sam Jun 25 '17 at 13:51
  • @sam It's because there are no students in `teilnehmer`. You create an array but you never populate it; never add any student to it – Mibac Jun 25 '17 at 13:56
  • but I have Students in my main as you can see it. How can I populate teilnehmer with those students from main? thanks – sam Jun 25 '17 at 13:58
  • @sam Define setter in your `Tutorium` class and perform `tutorium.setTeilnehmer(studs);` – Elena Larina Jun 25 '17 at 14:33
0

modify the code from this:

public String toString() {
    if (teilnehmer == null) {
        return "Noch keine Teilnehmer";
    }
    String out = "Tutorium " + fach + " Bei " + tutor + " in " + raumNr + " " + "\n";



    return out + --- ; //here i want to print all array element from teilnehmer
} 

to

@Override
public String toString() {
// if (teilnehmer == null) {
// return "Noch keine Teilnehmer";
// }
String out = "Tutorium " + fach + " Bei " + tutor + " in " + raumNr + " " + "\n";
return out + Arrays.toString(teilnehmer);
}

note that I commented out this (teilnehmer == null) because that condition will never met, you are creating an instance in the constructor so teilmehmer will never be null


you need to set the Teilnehmer:

public static void main(String[] args) {
// Studenten und Tutorien erzeugen.
Student[] studs = { new Student("Tim", 333333), new Student("Anna", 444444), new Student("Lisa", 345987),
    new Student("Karl", 336292), new Student("Elisabeth", 999999) };
Tutorium[] tuts = { new Tutorium(5, 6057, "Tobias", "PPR-J"), new Tutorium(2, 6057, "Roland", "PPR-C"),
    new Tutorium(9, 6051, "Max", "PPR-J") };
// Infos ueber Tutorien und Studenten ausgeben.
System.out.println("Es gibt " + Student.STUDENT_COUNT + " Studenten.");
//here set the array
for (Tutorium tutorium : tuts) {
    tutorium.setTeilnehmer(studs);
}
for (Student student : studs)
    System.out.println(student);
for (Tutorium tutorium : tuts)
    System.out.println(tutorium);
System.out.println("");
}

and define the setter:

public void setTeilnehmer(Student[] teilnehmer) {
    this.teilnehmer = teilnehmer;
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

Maybe you can try and override the toString-method in both the Student-class and the TutoriumsVerwaltung-class.

August Jelemson
  • 962
  • 1
  • 10
  • 29