I made an arraylist of objects which contain a question and an answer... How can I display just the question? Sooooo confused. Tutorials or anything will help, thanks in advance.

/* text file */
T Which Java keyword is used to define a subclass? extends 
S What is the original name of the Java language? ! *7 @ C-- # Oak $ Gosling 
M Which of the following types are supertypes of Rectangle? ! PrintStream @ Shape # RectangularShape $ Object % String 
N What is the square root of 2? 1.41421356
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package questions;

import java.util.ArrayList;
import java.util.Scanner;

/**
 *
 * @author Javier
 */
public class Quiz {
    private String quest;
    private String ans;
    ArrayList <Quiz> questions = new ArrayList<Quiz>();

    public String getAns() {
        return ans;
    }

    public String getQuest() {
        return quest;
    }
    
    public Quiz() {
    }

    
    
    public Quiz(String quest, String ans) {
        this.quest = quest;
        this.ans = ans;
    }
    
    
    
    
    public void read(Scanner in)
    {
        String wholeQA, asked, ans;
        String wholeQA2, asked2, ans2;
        String wholeQA3, asked3, ans3;
        String wholeQA4, asked4, ans4;
        wholeQA = in.next();
        asked = in.nextLine();
        ans = asked.substring(50);
        asked = asked.substring(1).replace(" extends", "");
        
        wholeQA2 = in.next();
        asked2 = in.nextLine();
        ans2 = asked2.substring(49);
        ans2 = ans2.replace("!", "A)").replace("@", " B)").replace("#", "C)").replace("$", " D) ");
        asked2 = asked2.substring(1).replace(" ! *7 @ C-- # Oak $ Gosling", "");
        
        
        
        wholeQA3 = in.next();
        asked3 = in.nextLine();
        ans3 = asked3.substring(59);
        ans3 = ans3.replace("!", "A)").replace("@", "B)").replace("#", "C)").replace("$", "D)").replace("%", "E)");
        asked3 = asked3.substring(1).replace(" ! PrintStream @ Shape # RectangularShape $ Object % String ", "");
        
        
        
        wholeQA4 = in.next();
        asked4 = in.nextLine();
        ans4 = asked4.substring(31);
        asked4 = asked4.substring(1).replace(" 1.41421356", "");
        
        
        questions.add(new Quiz(asked, ans));
        questions.add(new Quiz(asked2, ans2));
        questions.add(new Quiz(asked3, ans3));
        questions.add(new Quiz(asked4, ans4));
        
        System.out.println(questions.size());
        
    }
    
    public ArrayList<Quiz> getQuestions ()
    {
        return questions;
    }
    
    
    public String getText ()
    {
        String question = "";
        for (Quiz p : questions)
            question = p.getQuest();
            
        
        
        return question;
    }
    public boolean[] checkAnswers(ArrayList answers)
    {
        return;
    }
    
    public String getAnswer()
    {
        return;
    }
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package questions;

/**
 */
import java.io.FileReader; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Scanner; 
public class QuizRunner 
{ 
    public static void main(String[] args) 
    throws IOException 
    { 
        Quiz q = new Quiz(); 
        Scanner in = new Scanner(new FileReader("quiz.txt")); 
        q.read(in); 
        in.close(); 
        in = new Scanner(System.in); 
        ArrayList<Quiz> questions = q.getQuestions(); 
        ArrayList<String> answers = new ArrayList<String>(); 
        for (Quiz qu : questions) 
        { 
            System.out.println(qu.getText()); 
            String answer = in.nextLine(); 
            answers.add(answer); 
        } 
        boolean[] results = q.checkAnswers(answers); 
        for (int i = 0; i < results.length; i++) 
        { 
            System.out.println() ; 
            System.out. println(questions.get(i).getText()); 
            System.out.println("Correct answer: " + questions.get(i).getAnswer()); 
            System.out.println("Your answer: " + answers.get(i)); System.out.print("Your answer was "); 
            if (!results[i]) 
            { 
                System.out.print("not "); 
            } 
            System.out.println("correct."); 
        } 
} 
}

Edited 5 Years Ago by Nandomo: n/a

ArrayList <Quiz> questions = new ArrayList<Quiz>();

Every time you create a new Quiz, it has its own version of this ArrayList, so with n Quizes you have n ArrayLists, each with just one Quiz in it.

If you make it static, then there will be just one ArrayList that all the Quiz instances share, so all your new Quizes will go into that one ArrayList, and that will fix at least one of your problems!

Made the arrayList static and sent a counter in the quiz runner class. Any good? Any way to run a loop for display without sending a counter?

public String getText (int i)
    {
        
        String question = String.valueOf(questions.get(i).quest);
        if (String.valueOf(questions.get(i).type).equals("M") || 
            String.valueOf(questions.get(i).type).equals("S"))
        question = question.concat("\n" + String.valueOf(questions.get(i).ans));
            
        return question;    
    }

Ok. As you may see I am running a loop in the QuizRunner class and didn't know how to display only one question at a time from the getText method every time it loops... So I added a counter++ in the loop in QuizRunner and sent it to getText to use what I just showed you. So this is my question. Is there any way I can traverse the arrayList one element at a time without using a counter?

You can use a simple for-each loop

for (Quiz q : questions) { // "for each quiz q in questions"
  // do something with q
}

ps: Why all those String.valueOf calls when the data is a String anyway?

Lol. my IDE is freaking out and telling me they are Objects instead of Strings.

I see what you are telling me, but don't know how to regulate that without a counter. cuz the second time I call getText it would start at the first slot again

Here is all the code.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package questions;

import java.util.ArrayList;
import java.util.Scanner;

/**
 *
 * @author Javier
 */
public class Quiz {
    private String type;
    private String quest;
    private String ans;
    static ArrayList <Quiz> questions = new ArrayList<Quiz>();

    public String getAns() {
        return ans;
    }

    public String getQuest() {
        return quest;
    }
    
    public Quiz() {
        
    }

    
    
    public Quiz(String type, String quest, String ans) {
        this.quest = quest;
        this.ans = ans;
        this.type = type;
    }
    
    
    
    
    public void read(Scanner in)
    {
        String type, asked, ans;
        String type2, asked2, ans2;
        String type3, asked3, ans3;
        String type4, asked4, ans4;
        type = in.next();
        asked = in.nextLine();
        ans = asked.substring(50);
        asked = asked.substring(1).replace(" extends", "");
        
        type2 = in.next();
        asked2 = in.nextLine();
        ans2 = asked2.substring(49);
        ans2 = ans2.replace("!", "A)").replace("@", " B)").replace("#", "C)").replace("$", " D) ");
        asked2 = asked2.substring(1).replace(" ! *7 @ C-- # Oak $ Gosling", "");
        
        
        
        type3 = in.next();
        asked3 = in.nextLine();
        ans3 = asked3.substring(59);
        ans3 = ans3.replace("!", "A)").replace("@", "B)").replace("#", "C)").replace("$", "D)").replace("%", "E)");
        asked3 = asked3.substring(1).replace(" ! PrintStream @ Shape # RectangularShape $ Object % String ", "");
        
        
        
        type4 = in.next();
        asked4 = in.nextLine();
        ans4 = asked4.substring(31);
        asked4 = asked4.substring(1).replace(" 1.41421356", "");
        
        
        questions.add(new Quiz(type, asked, ans));
        questions.add(new Quiz(type2, asked2, ans2));
        questions.add(new Quiz(type3, asked3, ans3));
        questions.add(new Quiz(type4, asked4, ans4));
        
       
        
    }
    
    public ArrayList<Quiz> getQuestions ()
    {
        return questions;
    }
    
    
    public String getText (int i)
    {
        
        String question = questions.get(i).quest;
        if (questions.get(i).type.equals("M") || 
            questions.get(i).type.equals("S"))
        question = question.concat("\n" + questions.get(i).ans);
            
        return question;    
    }
        
        
    
    public boolean[] checkAnswers(ArrayList<String> answers)
    {
        boolean [] answer = new boolean [answers.size()];
        
        if (answers.get(0).equalsIgnoreCase("extends"))
            answer[0] = true;
        else if(answers.get(0).contains("extends") || answers.get(0).contains("EXTENDS"))
            answer[0] = true;
        else
            answer[0] = false;
        
        if (answers.get(1).equalsIgnoreCase("C"))
            answer[1] = true;
        else if(answers.get(1).contains("C") || answers.get(1).contains("c"))
            answer[1] = true;
        else
            answer[1] = false;
        
        if (answers.get(2).equalsIgnoreCase("BCD"))
            answer[2] = true;
        else if(answers.get(2).contains("BCD") || answers.get(2).contains("bcd"))
            answer[2] = true;
        else
            answer[2] = false;
        
        if (answers.get(3).equals("1.41421356"))
            answer[3] = true;
        else if(answers.get(3).contains("1.4"))
            answer[3] = true;
        else
            answer[3] = false;
        
        
        
        
        return answer;
    }
    
    public String getAnswer(int i)
    {
        String answer = "";
        if (i == 0)
            answer = "extends";
        else if (i == 1)
            answer = "C";
        else if (i == 2)
            answer = "BCD";
        else if (i == 3)
            answer = "1.41421356";
        
        return answer;
    }
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package questions;

/**
 */
import java.io.FileReader; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Scanner; 
public class QuizRunner 
{ 
    public static void main(String[] args) 
    throws IOException 
    { 
        int counter = 0;
        Quiz q = new Quiz(); 
        Scanner in = new Scanner(new FileReader("quiz.txt")); 
        q.read(in); 
        in.close(); 
        in = new Scanner(System.in); 
        ArrayList<Quiz> questions = q.getQuestions(); 
        ArrayList<String> answers = new ArrayList<String>(); 
        System.out.println("Instructions: On the multiple choice type in the letter, otherwise type out your answer.");
        for (Quiz qu : questions) 
        {
            System.out.println(qu.getText(counter));
            String answer = in.nextLine(); 
            answers.add(answer); 
            counter++;
        } 
        boolean[] results = q.checkAnswers(answers); 
        for (int i = 0; i < results.length; i++) 
        { 
            System.out.println() ; 
            //System.out. println(questions.get(i).getText()); 
            System.out.println("Correct answer: " + questions.get(i).getAnswer(i)); 
            System.out.println("Your answer: " + answers.get(i)); System.out.print("Your answer was "); 
            if (!results[i]) 
            { 
                System.out.print("not "); 
            } 
            System.out.println("correct."); 
        } 
} 
}

Edited 5 Years Ago by Nandomo: n/a

public String getText (int i)
public String getAnswer(int i)
(etc)

These are (correctly) instance methods - to call them you need an instance of Quiz, so it's a bad idea to have a parameter to identify which Quiz - in fact its completely unnecessary, just leave it out.
Eg instead of

public String getAnswer(int i)
{
  String answer = "";
  if (i == 0)
    answer = "extends";
  else(etc)
  return answer;
}

all you need is

public String getAnswer() {
   return ans:
}

Instead of the extra arrays for the user's answer and the correct/incorrect boolean, why not add an instance variable for the user's answer to the Quiz class, and have a boolean isCorrectAnswer() method that checks the user's answer against the correct answer and returns thre/false. That would make the final code much much simpler.

Finally, the biggest problem with this code is that you have included stuff about the individual answers into the code itself. That's a real no-no. If someone adds just one question to the questions file you;ll have to change your program. You have to remove every one of those details from the code and have a loop that just reads the file and processes each question one at at time, based only on what's in the file.

This question has already been answered. Start a new discussion instead.