0

I created a three-class program. In the third class, I should fill the array with the values "name", "surname", "code", and "age". What happens though is that my array is always "null", and I can't fill it. Thank you all for your help.

First class:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    String name, surname;
    int code, age, i=0, min=150;        
    
    System.out.println("How many teachers do you want to enter information?");
    i=in.nextInt();     

            
    for(int j=0; j<i; j++) 
    {
        System.out.println("Enter the teacher's name");
        name=in.next();
        System.out.println("Enter the teacher's surname");
        surname=in.next();
        System.out.println("Enter the teacher's code");
        code=in.nextInt();
        System.out.println("Enter the teacher's age");
        age=in.nextInt();
        
        Teacher d = new Teacher(name, surname, code, age);
        System.out.println(d.getCode());
        System.out.println(d.getSurname());
        System.out.println(d.getAge());
        
        University c = new University(name, surname, code, age);
        min=c.MinimumAge(age, min);

    }
    
    System.out.println(min);

}
}

Second class:

public class Teacher
{
    protected String name;
    protected String surname;
    protected int code;
    protected int age;
    
    
    public Teacher(String name, String surname, int code, int age)
    {
        this.name=name;
        this.surname=surname;
        this.code=code;
        this.age=age;
    }
    

    public String getName()
    {
        return name;
    }
    
    public int getCode()
    {
        return code;
    }
    
    public String getSurname()
    {
        return surname;
    }
    
    public int getAge()
    {
        return age;
    }
}

Third class (I put "0" as a location for filling the array just to do tests.):

public class University
{
    private Teacher[] array=new Teacher[4];;
    public University(String name, String surname, int code, int age)
    {
        array[0] = new Teacher(name, surname, code, age);
    }
    
    public int MinimumAge(int age, int min)
    {
        if (age<min)
            min=age;
        return min;
    }
}
9
  • None of the variables in the constructor of Teacher exist e.g. this.nome. Commented Dec 28, 2020 at 17:06
  • @LiveandLetLive Sorry, I translated the program from Italian. Now I've solved it. Thanks for notifying me. Commented Dec 28, 2020 at 17:09
  • 1
    "my vector is always "null"" What vector? Commented Dec 28, 2020 at 17:10
  • @Andreas Sorry, "array". Thank you Commented Dec 28, 2020 at 17:12
  • 1
    There is an array, created with size 4, and first element is assigned a value, so the other 3 will be null. What's surprising about that? Besides, how would you know, since you never use the array for anything? Commented Dec 28, 2020 at 17:13

1 Answer 1

1

The class University has not been implemented correctly. You can implement it as:

class University {
    private int counter = 0;
    private final int MAX = 4;
    private Teacher[] array = new Teacher[MAX];

    public void addTeacher(Teacher teacher) {
        if (counter < MAX) {
            array[counter++] = teacher;
        }
    }

    public int minimumAge() {
        if (counter == 0) {
            return 0;
        }
        int min = array[0].getAge();
        for (int i = 1; i < counter; i++) {
            if (array[i].getAge() < min) {
                min = array[i].getAge();
            }
        }
        return min;
    }
}

Accordingly, you need to change your code in main as shown below:

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String name, surname;
        int code, age, i = 0, min = 150;

        University university = new University();

        System.out.print("How many teachers do you want to enter information?: ");
        i = in.nextInt();

        for (int j = 0; j < i; j++) {
            System.out.print("Enter the teacher's name: ");
            name = in.next();
            System.out.print("Enter the teacher's surname: ");
            surname = in.next();
            System.out.print("Enter the teacher's code: ");
            code = in.nextInt();
            System.out.print("Enter the teacher's age: ");
            age = in.nextInt();

            university.addTeacher(new Teacher(name, surname, code, age));
        }

        System.out.println(university.minimumAge());
    }
}

A sample run:

How many teachers do you want to enter information?: 2
Enter the teacher's name: a
Enter the teacher's surname: b
Enter the teacher's code: 1
Enter the teacher's age: 23
Enter the teacher's name: x
Enter the teacher's surname: y
Enter the teacher's code: 2
Enter the teacher's age: 15
15
Sign up to request clarification or add additional context in comments.

Comments

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.