Array of Object
-
Hi, I have define the following class in my practice.
package array;
public class Student {// definition of fileds private int studentId= 0; private String name; private String family; //definition of constructor public Student(int \_studentId, String \_name, String \_family){ studentId= \_studentId; name= \_name; family= \_family; } //definition of methods public int getStudentId(){ return studentId; } public void setStudentId(int newStudentId){ studentId= newStudentId; } public String getName(){ return name; } public void setName(String newName){ name= newName; } public String getFamily(){ return family; } public void setFamily(String newFamily){ family= newFamily; }
}
then in main class I have these code
package array;
import java.io.*;
import array.Student;
public class Main {public static void main(String\[\] args) { //define Array of Object(Student's Class) Student\[\] st = new Student\[3\]; st\[0\]= new Student(3400,"XXX", "YYY"); st\[1\]= new Student(3800, "ZZZ", "WWW"); st\[2\]= new Student(4200,"RRR", "MMM"); System.out.println(st\[1\]); System.out.println("\\n"); }
}
the problem is after I run the program nothing print out on the screen. is there problem in code??
-
Hi, I have define the following class in my practice.
package array;
public class Student {// definition of fileds private int studentId= 0; private String name; private String family; //definition of constructor public Student(int \_studentId, String \_name, String \_family){ studentId= \_studentId; name= \_name; family= \_family; } //definition of methods public int getStudentId(){ return studentId; } public void setStudentId(int newStudentId){ studentId= newStudentId; } public String getName(){ return name; } public void setName(String newName){ name= newName; } public String getFamily(){ return family; } public void setFamily(String newFamily){ family= newFamily; }
}
then in main class I have these code
package array;
import java.io.*;
import array.Student;
public class Main {public static void main(String\[\] args) { //define Array of Object(Student's Class) Student\[\] st = new Student\[3\]; st\[0\]= new Student(3400,"XXX", "YYY"); st\[1\]= new Student(3800, "ZZZ", "WWW"); st\[2\]= new Student(4200,"RRR", "MMM"); System.out.println(st\[1\]); System.out.println("\\n"); }
}
the problem is after I run the program nothing print out on the screen. is there problem in code??
st[1] is object of type Student, not a String. You should have toString() method within your Student class
public String toString()
{
return "Name: " + name + ", " + family + " ID:" + studentId + "
}Than use simply use
System.out.println(st[1].toString());
It will start printing the student information.
-
st[1] is object of type Student, not a String. You should have toString() method within your Student class
public String toString()
{
return "Name: " + name + ", " + family + " ID:" + studentId + "
}Than use simply use
System.out.println(st[1].toString());
It will start printing the student information.
-
Hi, I have define the following class in my practice.
package array;
public class Student {// definition of fileds private int studentId= 0; private String name; private String family; //definition of constructor public Student(int \_studentId, String \_name, String \_family){ studentId= \_studentId; name= \_name; family= \_family; } //definition of methods public int getStudentId(){ return studentId; } public void setStudentId(int newStudentId){ studentId= newStudentId; } public String getName(){ return name; } public void setName(String newName){ name= newName; } public String getFamily(){ return family; } public void setFamily(String newFamily){ family= newFamily; }
}
then in main class I have these code
package array;
import java.io.*;
import array.Student;
public class Main {public static void main(String\[\] args) { //define Array of Object(Student's Class) Student\[\] st = new Student\[3\]; st\[0\]= new Student(3400,"XXX", "YYY"); st\[1\]= new Student(3800, "ZZZ", "WWW"); st\[2\]= new Student(4200,"RRR", "MMM"); System.out.println(st\[1\]); System.out.println("\\n"); }
}
the problem is after I run the program nothing print out on the screen. is there problem in code??
future3839 wrote:
System.out.println(st[1]);
Presumably you are using the standard Sun/Oracle VM. If not then you need to specify what you are using. Modify this line so it prints something such, for example. System.out.println("student 1= " + st[1]); There are two possibilities. 1. Your program still prints nothing. Thus you are not running the code you think you are. 2. It prints the text but not else. That suggests you have a toString() method in the class that prints nothing.
-
Hi, I have define the following class in my practice.
package array;
public class Student {// definition of fileds private int studentId= 0; private String name; private String family; //definition of constructor public Student(int \_studentId, String \_name, String \_family){ studentId= \_studentId; name= \_name; family= \_family; } //definition of methods public int getStudentId(){ return studentId; } public void setStudentId(int newStudentId){ studentId= newStudentId; } public String getName(){ return name; } public void setName(String newName){ name= newName; } public String getFamily(){ return family; } public void setFamily(String newFamily){ family= newFamily; }
}
then in main class I have these code
package array;
import java.io.*;
import array.Student;
public class Main {public static void main(String\[\] args) { //define Array of Object(Student's Class) Student\[\] st = new Student\[3\]; st\[0\]= new Student(3400,"XXX", "YYY"); st\[1\]= new Student(3800, "ZZZ", "WWW"); st\[2\]= new Student(4200,"RRR", "MMM"); System.out.println(st\[1\]); System.out.println("\\n"); }
}
the problem is after I run the program nothing print out on the screen. is there problem in code??
If I run your code I get the following output:
testing.BaseTest$Student@1ad77a7
However, If I take Shivamkalra's advice and add a ToString() method to the Student class such as:
public String toString()
{
return "Name: " + name + ", " + family + " ID:" + studentId + "
}I get the following output:
Name: ZZZ, WWW ID:3800
I must get a clever new signature for 2011.
-
st[1] is object of type Student, not a String. You should have toString() method within your Student class
public String toString()
{
return "Name: " + name + ", " + family + " ID:" + studentId + "
}Than use simply use
System.out.println(st[1].toString());
It will start printing the student information.
perfect answer! @ future3839 just a short note: your code looks really good, clean and well structured. One small point I noticed is the package declaration:
package array;
. It would be more appropriate to use a longer declaration likepackage au.future3839.school.arrays;
. this would define your package clearly as your package, otherwise you could get into trouble when having a lucky hit on a predefined package. regards TorstenI never finish anyth...
-
Hi, I have define the following class in my practice.
package array;
public class Student {// definition of fileds private int studentId= 0; private String name; private String family; //definition of constructor public Student(int \_studentId, String \_name, String \_family){ studentId= \_studentId; name= \_name; family= \_family; } //definition of methods public int getStudentId(){ return studentId; } public void setStudentId(int newStudentId){ studentId= newStudentId; } public String getName(){ return name; } public void setName(String newName){ name= newName; } public String getFamily(){ return family; } public void setFamily(String newFamily){ family= newFamily; }
}
then in main class I have these code
package array;
import java.io.*;
import array.Student;
public class Main {public static void main(String\[\] args) { //define Array of Object(Student's Class) Student\[\] st = new Student\[3\]; st\[0\]= new Student(3400,"XXX", "YYY"); st\[1\]= new Student(3800, "ZZZ", "WWW"); st\[2\]= new Student(4200,"RRR", "MMM"); System.out.println(st\[1\]); System.out.println("\\n"); }
}
the problem is after I run the program nothing print out on the screen. is there problem in code??