-1

Can someone explain why nothing is printing when I run this program?

public class Teacher {

public static void main(String [] args) {

   Teacher [] teacher = new Teacher[2];
   teacher[0] = new Teacher ("Jack Campbell", 74);
   teacher[1] = new Teacher ("Mary Smith", 69;
   }
}


public class testTeacher {

   String name;
   int age;

   public testTeacher (String name, int age) {
   }

   public String toString() {
     String teacher = name;
     teacher += getAge();
     teacher += "Age is" + age;
     return teacher;
   }

   public void setName (String nam) {
      name = nam;
   }

   public String getName() {
      return name;
   }

   public void setAge(int Age){
      age = Age;
   }

   public String getAge() {
      return age;
   }
}

What am I missing here? I think it has something to do with the toString method but I'm not positive. In my code it says the method is never used an I'm unsure why.

Mandar Pandit
  • 2,171
  • 5
  • 36
  • 58
Ralphie
  • 13
  • 2
  • Where is your main method??? – NewBee Developer Dec 15 '15 at 09:08
  • Edited it in, totally forgot. It compiles and builds but when it runs doesn't output anything. All I'm basically trying to do is print out the teachers name and age using toString. Figure I'll have to use a for loop as well. – Ralphie Dec 15 '15 at 09:09
  • 1
    First off you're missing a closing bracket for your `toString()` method (you're also missing an `@Override` annotation but that's more of a style issue). You're never setting the values of `name` or `age` in your constructor, so they will always be blank until their respective setters are called. You're adding the teacher's age to the string twice, which you're probably not intending to do. The `main` method you've added is also missing a closing bracket. All that said and done, you're never calling the `toString()` method of either object. – JonK Dec 15 '15 at 09:12
  • I can fix all of that easily...I just don't understand how you go about calling the toString() method to an object. How does that work? Also I'm not quite what you mean by not setting the values of name/age. Isn't it ok if they're blank until the getter/setters are called? – Ralphie Dec 15 '15 at 09:15
  • You do it the same way you call any other method on an object: get a reference to the object you want to call `toString()` on, then tack `.toString()` onto it, so for example `teacher[0].toString();`. Bear in mind that this *doesn't print to the console*. You'll need to use something like `System.out.println()` for that. With respect to the variables being blank; the point of constructors is to set up the object you're creating so that it can be used, part of that is to properly set the values of the instance variables. – JonK Dec 15 '15 at 09:19
  • But how can you set them if they have different values? For example the age is going to be different for every person obviously. – Ralphie Dec 15 '15 at 09:24
  • Because your variables aren't marked as `static` each instance of your class has its own copies of those variables. That allows each instance to have different values set for them without affecting any of the other instances. If you had marked them as `static` their values would be shared across all of the instances (and that would be a bug in your case). – JonK Dec 15 '15 at 09:36

2 Answers2

0

This is how you can write :

class Teacher {

  public static void main(String [] args) {
     Teacher [] teacher = new Teacher[2];
     teacher[0] = new Teacher ("Jack Campbell", 74);
     teacher[1] = new Teacher ("Mary Smith", 69);
     System.out.println(teacher[0].toString());
  }

  String name;
  int age;

  public Teacher (String name, int age) {
     this.name = name;
     this.age = age;
  }

  @Override
  public String toString() {
     String teacher = name;
     teacher += "Age is" + age;
     return teacher;
  }
}
Mandar Pandit
  • 2,171
  • 5
  • 36
  • 58
  • So that solves the constructor problem. Thanks a lot :) If I wanted to print both the teachers using a for loop `(int i=0; i < teacher.length; i++) {` `System.out.println(teacher[].toString());` ??? – Ralphie Dec 15 '15 at 09:41
  • Got it nevermind. Thanks for the help :) – Ralphie Dec 15 '15 at 09:55
0

If you want to print all the teachers in the array:

for (int i=0; i < teacher.length; i++) { 
System.out.println(teacher[i].toString();)
}

I would recommend you doing some basic java tutorials about object orientied programming, arrays and loops. Also be sure to use a good development environment like eclipse (freeware) to get good code highlighting and meaningful error messages.

Richard Loth
  • 79
  • 1
  • 7