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;
    }
}

You can read a string from a file and trim leading a trailing spaces "using trim()".
You can use split("<delimiter goes here>") on Strings
You can use charAt(num) on strings to get a specific character.

import java.io.*;

public class DW_391778
{
   private static void DemoTrim(String strData)
   {
      System.out.println('['+strData.trim()+']');
   }

   private static void DemoSplit(String strData)
   {   //http://www.java-examples.com/java-string-split-example
      String[] arr_strData = strData.split(" ");

      for(String s : arr_strData)
      {
         if(!s.equals(""))
         {
            System.out.println('['+s+']');
         }
      }
   }

   private static void DemoCharAt(String strData)
   {
      System.out.println(strData.trim().charAt(8)); //prints 'n'
   }


   public static void main(String[] args)
   {
     String strData = "   this is neat   ";
      System.out.println("** DemoTrim **");
      DemoTrim(strData);

      System.out.println("** DemoSplit **");
      DemoSplit(strData);

      System.out.println("** DemoCharAt **");
      DemoCharAt(strData);
   }
}
Member Avatar for hfx642

If all of the input lines, of the text file, are the same... it should be pretty obvious.
Read in the string and chop off the first 2 characters.

This any good?

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

Well, what you have used is correct functions of String. If it works for you, its great. but i have a question. Why do you need four sets of

String type, asked, ans;

to perform the same operation.

Once a set of values required for your quiz object is ready. Create the quiz object and add that to questions and use the same references.

What would you do if your text file had 25 questions ? 25 sets of reference variables?

Apart from that, a string also creates a new memory for almost every single operation.

I would use one set of

StringBuilder type = new StringBuilder();
StringBuilder asked = new StringBuilder();
StringBuilder ans = new StringBuilder();

objects and reuse them for the next set of operations.

Saves memory and advised way of programming.

I appreciate all the help guys.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.