0

I keep getting the output Student@42a57993. I would like to print out the results that is in students[0], I also would like to print out all the elements in the students array, Can someone help me and tell me whats wrong? i have one main method class which is the Client and another class which is Student.

    public class Client {

        public static void main(String[] args){

            Student[] students = new Student[3];
            students[0] = new Student();
            students[0].StudentParticulars();
            System.out.println((students[0]).toString());

        }
    }


import java.util.Scanner;

    public class Student {

        Scanner userInput = new Scanner(System.in);
        String studName;

        public void StudentParticulars(){
            System.out.println("Enter Student name: ");
            studName = userInput.nextLine();
        }
    }
jgm
  • 1,230
  • 1
  • 19
  • 39
  • Strudent is a class, and Students[0] is an instance of if. – ogdabou Oct 28 '17 at 13:20
  • In your case students[0]).toString() is calling toString method of Object class. One way to get all elements of Student class is to override toString() method in Student class like below @Override String toString() { return "studName = "+ this.studName; } Other way is to have a method which will just print all elements of Student class. To print all elements of students array, you can use code below : for(Student student : students) { System.out.println(student); } Above code assumes that you overrides toString method in Student class – abhig Oct 28 '17 at 13:29

1 Answers1

0

Student is an object and you have to use the member functions in the that class to print out the contents of the object.

jgm
  • 1,230
  • 1
  • 19
  • 39
  • Can you please share how can I go about it. I am basically supposed to collect the userinput on student names and grades and put them in an array. I would need to create a new student when the user wants to add a new student with the names and grades they have into my program. So, I was trying to do the above and i am just unable to get it. – LearningInProgress Oct 28 '17 at 13:31
  • @ASHWIIN you need to show the student class as well. – jgm Oct 28 '17 at 20:37