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.");
}
}
}