I am writing a program that gives and grades a test. Currently I am working on reading in the text from a file to administer the test, but am running into limitations. So I have a couple of questions. Is there any way I can read in a segment of text without picking the first and last spaces? If not, can anyone link me to a good split tutorial. Also is there any way to read in up to a certain char like in CPP? My Code is below, its 2 classes and the text file, any help appreciated.
/* 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
/*
* Class QuizRunner
*
*/
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."); } } }
//You need to take an OO approach to the code.
/*
* Class Quiz
*
*/
package questions;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
*
*/
public class Quiz {
public void read(Scanner in)
{
String into, out;
into = in.next();
out = in.nextLine();
System.out.println(into);
System.out.println(out);
}
public ArrayList<Quiz> getQuestions ()
{
ArrayList <Quiz> arl = new ArrayList<Quiz>();
arl.
return;
}
public String getText ()
{
return;
}
public boolean[] checkAnswers(ArrayList answers)
{
return;
}
public String getAnswer()
{
return;
}
}