0
public class Question{
    private String question;
    private String[] answers;
    public int answerPosition;
    
    public Question(String question, int size){
        this.question = question;
        this. answers = new String[size];
        answerPosition = 0;              
    }

    public void addAnswer(String answer){
        answers[answerPosition] = answer;
        answerPosition++;
    }
}

public class Quiz{
    private ArrayList<Question> question = new ArrayList<>();;
    private Scanner scanner = new Scanner(System.in);

    public Quiz(){    
    }
    public void addQuestion(String newQuestion, int size){
        Question ques = new Question(newQuestion, size);
        for(int i = 0; i<size; i++){
            System.out.println("Enter answer");
            String answer = scanner.nextLine();
            ques.addAnswer(answer);
        } 
        question.add(ques);    
    }

   public void printQuestion(){
        for(int i = 0; i < question.size(); i++){
            System.out.println(question.get(i));        
        }
    }
}

//When ever it prints out it prints the hash code. I have looked for similar answers but I think its because the question constructor contains strings and arrays and I haven't been able to find any similar solutions.

//I have tried using a toString() method in the question class but I couldn't get that to work either. Any help on this would be greatly appreciated I literally have no hair left lol

azro
  • 53,056
  • 7
  • 34
  • 70
DaveM86
  • 3
  • 2
  • As you know that you need to implement `toString`, please show your implementation and explain how it "didn't work". – tgdavies Jun 18 '22 at 08:14
  • I managed to fix it but it wasn't using a toString() it seemed all I needed to do was create a print method in the question class that printed out the question and looped through the answers, I think I was overthinking it because the solution looks so obvious now lol. still all new to me but thanks anyways – DaveM86 Jun 19 '22 at 06:50

0 Answers0