view FileList.java @ 45:351b0c8b34ac

Fixed issue where error message was shown every time the user chose to move continue using the keyboard. Fixed issue where data wasn't saving to file after questionaire. Changed SubjectData output filename to "<Participant ID>_qu.dat" (previously was "subjects.dat". Would have been overwritten as wasn't dynamic.
author Carl Bussey <c.bussey@se10.qmul.ac.uk>
date Sun, 09 Jun 2013 22:06:52 +0100
parents f0cd8f32e8c8
children
line wrap: on
line source
/*=============================================================================
 * File:       FileList.java
 * Author:     Marcus Pearce <m.pearce@gold.ac.uk>
 * Created:    <2007-11-30 10:30:38 marcusp>
 * Time-stamp: <2012-03-01 09:35:00 marcusp>
 *=============================================================================
 */

import java.util.ArrayList; 
import java.util.Iterator;

import java.io.*;

import java.util.Random;

public class FileList { 

    private ArrayList files; 
    private ArrayList probes; 
    private int numFiles; 
    private int currentFileIndex;

    public int numFiles() { return numFiles; }
    public void setFileIndex(int i) { currentFileIndex = i; }

    public String currentFile() { 
        if (currentFileIndex < numFiles)
            return (String)files.get(currentFileIndex); 
        else 
            return null; 
    }

    public ArrayList currentProbes() { 
        if (currentFileIndex < numFiles)
            return (ArrayList)probes.get(currentFileIndex); 
        else 
            return null; 
    }
     
    public void incrementFileIndex() { 
        currentFileIndex = currentFileIndex + 1; 
    }

    private void randomise() { 
        Random rgen = new Random();  
        for (int i = 0; i < numFiles; i++) {
            int randomPosition = rgen.nextInt(numFiles);
            // System.out.println("i = " + i + "; rpos = " + randomPosition + 
            //                    "; files = " + files.size() + 
            //                    "; probes = " + probes.size());
            
            String tempFile = (String)files.get(i);
            files.set(i, files.get(randomPosition)); 
            files.set(randomPosition, tempFile); 
            
            ArrayList tempProbe = (ArrayList)probes.get(i); 
            probes.set(i, probes.get(randomPosition));
            probes.set(randomPosition, tempProbe); 
        }
    }
    
    private void importFileList(String filename) {
        try { 
            LineNumberReader lnr = 
                new LineNumberReader(new FileReader(new File(filename)));
            lnr.setLineNumber(1);
            StreamTokenizer stok = new StreamTokenizer(lnr);
            stok.parseNumbers();
            stok.wordChars('_','_'); 
            stok.wordChars('0','9');
            //stok.wordChars('/','/');             
            stok.eolIsSignificant(true);
            stok.nextToken();
            while (stok.ttype != StreamTokenizer.TT_EOF) {
                int lineno = lnr.getLineNumber();
                int i = 0;
                ArrayList iprobes = new ArrayList(); 
                while (stok.ttype != StreamTokenizer.TT_EOL) {
                    int j = 0; 
                    if (stok.ttype == StreamTokenizer.TT_NUMBER) { 
                        iprobes.add((int)stok.nval - 1); // zero indexing
                    } else if (stok.ttype == StreamTokenizer.TT_WORD) { 
                        files.add(stok.sval);
                    }
                    stok.nextToken();     
                }
                i++; 
                numFiles++; 
                probes.add(iprobes); 
                stok.nextToken();
            }
        } catch(Exception e) {
            System.out.println("Exception: " + e);
        }
    }

    public void print() { 
        Iterator fi = files.iterator(); 
        Iterator pi = probes.iterator(); 
        while (fi.hasNext()) {
            System.out.print((String)fi.next() + ": "); 
            ArrayList ip = (ArrayList)pi.next();

            for (int i = 0; i < ip.size(); i++) { 
                int ppos = ((Integer)ip.get(i)).intValue() + 1;
                System.out.print(ppos + " ");
            }
            System.out.print("\n");
        }
    }

    public FileList(String filename) { 
        files = new ArrayList(); 
        probes = new ArrayList(); 
        currentFileIndex = 0; 
        this.importFileList(filename);
        this.randomise(); 
        this.print(); 
    }
}