Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Array of Object

Array of Object

Scheduled Pinned Locked Moved Java
javadata-structureshelpquestion
7 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    future3839
    wrote on last edited by
    #1

    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??

    S J L S 4 Replies Last reply
    0
    • F future3839

      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??

      S Offline
      S Offline
      shivamkalra
      wrote on last edited by
      #2

      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.

      J T 2 Replies Last reply
      0
      • S shivamkalra

        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.

        J Offline
        J Offline
        jschell
        wrote on last edited by
        #3

        Shivamkalra wrote:

        st[1] is object of type Student, not a String

        Which wouldn't stop it from using the default toString() method which would print something.

        1 Reply Last reply
        0
        • F future3839

          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??

          J Offline
          J Offline
          jschell
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • F future3839

            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??

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • S shivamkalra

              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.

              T Offline
              T Offline
              TorstenH
              wrote on last edited by
              #6

              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 like package 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 Torsten

              I never finish anyth...

              1 Reply Last reply
              0
              • F future3839

                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??

                S Offline
                S Offline
                shi fj
                wrote on last edited by
                #7

                st[1] is a object,not primitive type.So it need a print method. :) from china

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups