0

I am trying to display the different objects in an ArrayList. In my project context, one student is one object.

I used an ArrayList to store all the different student objects and I am having problems reading the ArrayList.

 <%
   String student_name = request.getParameter("studentName"); 
  ArrayList<Object[]> studentList = new ArrayList<Object[]>();
  if(student_name != null && student_name.length() > 0) {
  PreparedStatement preparedStatement = con.prepareStatement("Select * from      users where firstname LIKE ? ");
  preparedStatement.setString(1, "%" +student_name+ "%");
  ResultSet resultSet = preparedStatement.executeQuery();

  while (resultSet.next()) {
      String first_name = resultSet.getString("firstname");
      String last_name = resultSet.getString("lastname");
      String email = resultSet.getString("email");

      Object[] student = {first_name,last_name,email};
      studentList.add(student);
      session.setAttribute("studentObject",studentList);


      //System.out.println("First Name: " + first_name + "," + "Last Name: " + last_name);
      System.out.println(studentList.get(0));


  }

When I try to display (studentList.get(0)), all I see is "[Ljava.lang.String;@XXXX"

How do i get it to display the different student objects based on the index ?

purplewind
  • 331
  • 10
  • 26
  • 1
    You got the student object. The output tells you that it is an array of strings at address XXXXX. To get the elements of the student object (e.g. first_name), you have to access the appropriate index: `System.out.println(studentList.get(0)[0]);` – Quagaar Nov 14 '16 at 15:51
  • What are you trying to do? You're just getting the result of the `Object[]`'s toString method, which is what you see. What would you like? – Taylor Nov 14 '16 at 15:52
  • Are you doing that inside JSP page? – ppopoff Nov 14 '16 at 15:56

5 Answers5

1

At first, It will be more idiomatic in Java to define your own class Student. Write an extractor to that class, define toString method and it will be great.

Java requires you to define toString method to any type of object that will be printed. So, you have to define toString for your Student class.

But you are using an array. Java doesn't have toString method defined for Arrays. So I would propose you to do something like this. (I'm a bit on rush so code may contain some mistakes:

// At first it could be better to define a structure
// that represents a student
// class must be defined in separate file like Student.java
// Student.java
public class Student {
   // constructor for the Student object
   public Student(final String firstName,
                  final String lastName,
                  final String email) {

       this.firstName = firstName;
       this.lastName  = lastName;
       this.email     = email;
   }

   private String firstName;
   private String lastName;
   private String email;

   @override String toString() {
       return firstName + " " + lastName + " " + email;
   }

   // getters
   public String getFirstName() { return firstName; }
   public String getLastName() { return lastName; }
   public String getEmail() { return email; }

   // setters
   public void setFirstName(final String firstName) {
       this.firstName = firstName;
   }

   public void setLastName(final String lastName) {
       this.lastName = lastName;
   }

   public void setEmail(final String email) {
       this.email = email;
   }
} // end of Student.java file
///////////////////////////////////////////

final ArrayList<Student> studentList = new ArrayList<Student>();

.... // your code

ResultSet resultSet = preparedStatement.executeQuery();

while (resultSet.next()) {
   // creating a new student with new keyword
   final Student currentStudent = new Student(
       resultSet.getString("firstname"),
       resultSet.getString("lastname"),
       resultSet.getString("email")
   )

   // you are adding a student object to the list
   studentList.add(currentStudent);
   session.setAttribute("studentObject",studentList);

   // that should work
   // toString method will be called implicitly
   System.out.println(studentList.get(0));
}

// So you will have a list of students
// that will be accessable in the following manner:

studentList.get(0).getFirstName;
studentList.get(1).setFirstName("New name");

If you want a different behaviour you may call those fields directly, or modify the behavior of toString method

Java also assumes that you're using camelCase notation, you may find in in the style guide.

Hope it helps.

ppopoff
  • 658
  • 7
  • 17
0

Try java.util.Arrays class:

System.out.println(Arrays.toString(studentList.get(0)));
SeniorJD
  • 6,946
  • 4
  • 36
  • 53
0

Currently you are printing out the Object[] which is not exactly human readable. I would suggest creating a new class called Student. Then have three member variables firstName, lastName, and email. Also create a toString() method for this new class. Then you can have an ArrayList of Students. This should simplify everything for you.

Joe
  • 800
  • 4
  • 10
  • 26
0

It looks like you're not trying to display a String as you expect, you're trying to display an array of Objects.

You could try : System.out.println(studentList.get(0)[0]);

You'll have an idea

nick zoum
  • 7,216
  • 7
  • 36
  • 80
0

You have at least two options:

  1. Implement an object corresponding to Object[] student:
    In this class you may override Object's toString(), and implement it as to wish to present the data. NOTE: this is very relevant to you because when printing studentList.get(0) you actually print call Object's default toString() which returns the reference to the object.

    For more information regarding default Object.toString() here.

  2. The simplestway is to use Arrays.toString():
    System.out.println(Arrays.toString(studentList.get(0)));

Community
  • 1
  • 1
Gal Dreiman
  • 3,969
  • 2
  • 21
  • 40