views:

44

answers:

2

I'm building a CLI survey app in Java that lets users answer multiple-choice questions. Please tell me how my approach can be improved.

Each answer to a question is recorded as an int, which is the index of that choice in the array of choices for that question. All answers for a survey are saved as an int array. I'm thinking of serializing this int array using ObjectOutputStream. Then when I need to display the results, I'll restore the array and print each element after a tab.

The problem with this approach is (I believe) the second set of responses overwrites the first. I thought of two alternatives, both of which sound bad as well. One is saving each response set to a separate file. But then come display time, I'll have to read all the files at once to keep responses for the same question together. The other is saving the int array as a tab-separated line in a plain text file (so each response set makes a new line), then tokenize and parse it back to an int array for displaying. But then the tokenizing/parsing code is a horror to read (currently it looks like this):

EDIT: Whoops, this code isn't even right. My point is just that parsing is a mess, so I'm looking for a way not to have to parse.

File savedResults = new File(Main.surveyResultsFolder, surveyFileName);
try {
    BufferedReader br = new BufferedReader(new FileReader(savedResults));
    int[] currentResponseSet = new int[noOfQuestions];
    String currentResponseString = "";
    String[] currentResponseStrArray = null;
    while((currentResponseString = br.readLine()) != null) {
        currentResponseStrArray = currentResponseString.split("\t");

    for (int i = 0; i < currentResponseStrArray.length; i++) {
        currentResponseSet[i] = Integer.parseInt(currentResponseStrArray[i]);
}
    }
     //then I'll print currentResponseSet here.
    }
catch (IOException ex) {
    System.out.println("Error reading results file " + surveyFileName);
}

I'm out of ideas. As you can see, my knowledge of data handling techniques is limited. Any takers?

+3  A: 

The most obvious thing to do is to save it to a database. Then storage and retrieval is very straightforward.

If you can't do that for whatever reason, then I think your present solution is the way to go. Your parsing code looks pretty straightforward to me. I'd create a separate function to parse a line that returns the array of responses for one line, and then have the caller do whatever needs to be done with it, just for clean modularization.

Jay
Thanks! The only reason I'm not using a DB is because I don't know how to use one :">
Anita
@Anita: Well, that's a good reason if you're looking for a quick, short-term answer. In the longer term, I suggest learning to use a database! I don't know what you're hosting arrangement is, but you can get MySQL or Postgres for free, and any hosting company I've ever used offers at least one of those as part of the package.
Jay
+1  A: 

Several points:

  1. Saving data using an ObjectOutputStream is generally a bad idea. Saving data in human readable form is a superior choice.
  2. I don't understand why you think the parsing logic you have there is 'a horror' -- sure you can clean the code up a bit, but fundamentally, it seems ok.
  3. Have you considered storing the data in a database... relational or otherwise?
  4. Instead of using the array index position to tie an answer to a question, I would use question IDs. That way, the survey can evolve over time and you will still have semi-usable data. At the very least, you will have a partially complete answers. If you want minimalistic persistence form for such a structure, one idea is to store { record, question, answer } in each line of text. Reading the data would produce Map<record, Map<question, answer>>. You may find an 'automap' construct useful here:
    public abstract class AutoMap<K extends Comparable, V> 
    extends TreeMap<K, V> {
      protected abstract V createValue();

      public V getOrCreate(K key) {
        V value = get(key);
        if (value == null) {
          value = createValue();
          put(key, value);
        }
        return value;
      }
    }

    public class MapOfMaps<J extends Comparable, K extends Comparable, V> 
    extends AutoMap<J, Map<K, V>> {
      protected Map<K, V> createValue() {
        return new TreeMap<K, V>();
      }
    }
Dilum Ranatunga
That does sound like a good solution! If I'm called on to improve the code above I'll be sure to use it :)
Anita