1

I'm trying to create an object of type quiz that can hold 25 objects, but whenever I print the array, I get a pointer location (random numbers "Quiz@471e30" ) instead of my string question.

Here is the Quiz class:

public class Quiz {
    private static String questions;

    public  Quiz (String ask){
        questions=ask;
    }

    public String getQuestions(){
        return questions;
    }

    public void setQuestions(String ask){
        questions=ask;
    }
}

Here is the main class:

public class QuizTime {

    public static void main(String[] args) {

        Quiz[] z= new Quiz[25];
        z[0]=new Quiz("what is your name?");
        System.out.println(z[0]);
    }
}
Chris J
  • 30,688
  • 6
  • 69
  • 111
user3500147
  • 125
  • 1
  • 11

5 Answers5

1

Implement the method toString in your Quiz class and return the appropriate string to be printed.

public String toString() {
   return questions;
}

Of course this makes your getQuestions() method a little redundant, so you could also call the function inside toString(), or go with one of the other answers which explicitly calls getQuestions().

public String toString() {
   return getQuestions();
}
merlin2011
  • 71,677
  • 44
  • 195
  • 329
  • Also refer to http://stackoverflow.com/questions/3615721/how-to-use-the-tostring-method-in-java – vda8888 Jun 23 '14 at 20:26
0

Presuming your existing works, you will need to add a method call to the object reference. Something like z[0].getQuestion(). Alternatively you could define a cusom .toString() method to return the question.

IdusOrtus
  • 1,005
  • 1
  • 16
  • 24
0

Without the toString() method, Java automatically prints out the class's memory address with System.out.println(). You can implement toString() or do this:

System.out.println(z[0].getQuestions());
yizzlez
  • 8,757
  • 4
  • 29
  • 44
0

z[0] is the quiz object, while what you are trying to print is the String named questions. Try instead:

System.out.println(z[0].getQuestions());

Alternatively, you could override the toString() method, which would then print the questions if you entered:

System.out.println(z[0]);

Patrick
  • 31
  • 4
0

In addition to the String being from Object#toString(), which the Javadoc describes as -

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode())

You also have a static field which limits your Quiz to one (and only one) question. It should probably be,

public class Quiz {
  private String questions;
  public  Quiz (String ask){
    setQuestions(ask);
  }
  public String getQuestions(){
    return this.questions;
  }
  public void setQuestions(String ask){
    this.questions=ask;
  }
  @Override
  public String toString() {
    return getQuestions();
  }
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249