i may already have asked a similar question to this but my brain is so fried with this problem.
i have to read two csv files, entering the data contained into a map and a list.
the first method readInTasks() works fine, reads the file and enters the data into the map. the second method readInAttempts() works to the point of putting the data into the list... i've tried alsorts. i've tried add, set, put but these just return the cannot find the method add() error message. can someone please give me a pointer as to what this should be?
code below
import java.util.*;
import java.io.*;
import ou.*;
/**
* Class TrialAdmin - write a description of the class here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class TrialAdmin
{
/* instance variables */
private Map<Integer, Integer> targetMap;
private List<Attempt> attempts;
/**
* Constructor for objects of class TrailAdmin
*/
public TrialAdmin()
{
super();
targetMap = new TreeMap<Integer, Integer>();
attempts = new ArrayList<Attempt>();
}
/* instance methods */
/**
* Display the targetMap on the default output
*/
public void displayTargets()
{
System.out.println(targetMap);
}
/**
* Prompts the user for a pathname and then attempts to open a stream
* on the specified file. The method expects a file containing the details
* of target values in CSV format and will return a collection of TrialAdmin objects
* constructed from those details
*/
public void readInTasks()
{
String pathname = OUFileChooser.getFilename();
File aFile = new File(pathname);
BufferedReader bufferedFileReader = null;
int taskNumber;
String version;
int pitchTarget;
try
{
bufferedFileReader = new BufferedReader(new FileReader(aFile));
String currentLine = bufferedFileReader.readLine();
while (currentLine != null)
{
Scanner lineScanner = new Scanner(currentLine);
lineScanner.useDelimiter(",");
taskNumber = lineScanner.nextInt(); //return the next token as an integer
version = lineScanner.next(); // return the next token as a string
pitchTarget = lineScanner.nextInt(); // return the next token as an int
targetMap.put(taskNumber, pitchTarget);
currentLine = bufferedFileReader.readLine(); //get the next line
}
}
catch (Exception anException)
{
System.out.println("Error: " + anException);
}
finally
{
try
{
bufferedFileReader.close();
}
catch (Exception anException)
{
System.out.println("Error: " + anException);
}
}
}
public void readInAttempts()
{
String pathname = OUFileChooser.getFilename();
File aFile = new File(pathname);
BufferedReader bufferedFileReader = null;
try
{
String currentLine;
bufferedFileReader = new BufferedReader(new FileReader(aFile));
currentLine = bufferedFileReader.readLine();
while (currentLine != null)
{
Scanner lineScanner = new Scanner(currentLine);
lineScanner.useDelimiter(",");
int taskNumber = lineScanner.nextInt(); //return the next token as an integer
Double pitchAttempt = lineScanner.nextDouble(); // return the next token as a double
if (pitchAttempt <= 0)
{
String pitch;
pitch = OUDialog.request("Please enter a value for task " + taskNumber);
currentLine = taskNumber + "," + pitch;
}
int pitchTarget = this.targetMap.get(taskNumber); //this.getPitchTarget();
currentLine = currentLine + "," + pitchTarget;
System.out.println(currentLine);
//all versions of this commented out!
//Attempt attempts = new Attempt(taskNumber, pitchAttempt, pitchTarget);
//attempts.add(currentLine);
//attempts.add(taskNumber, pitchAttempt, pitchTarget); //this was the way i thought it should be coded
currentLine = bufferedFileReader.readLine(); //get the next line
}
}
catch (Exception anException)
{
System.out.println("Error: " + anException);
}
finally
{
try
{
bufferedFileReader.close();
}
catch (Exception anException)
{
System.out.println("Error: " + anException);
}
}
}
// public void readInAttempts()
// {
// this.attempts = Test.readAttempts(this.targetMap);
// }
}
()
/**
* Class Attempt - instances of this class record
* an attempt to reproduce a musical note vocally.
*
* @author M255 Course Team
* @version Version 1.0
*/
public class Attempt
{
/* instance variables */
private int taskNumber; // task number
private double pitchAttempt; // number representing result of task
// e.g. if target was 35, value of
// pitchAttempt might be 34.63 or 35.51
private int pitchError; // difference between target and attempt
// multiplied by 100 and rounded to nearest integer
private int pitchTarget; // target pitch used in task
/**
* constructor for objects of class Attempt
*/
public Attempt(int task, double attempt, int target)
{
super();
this.taskNumber = task;
this.pitchAttempt = attempt;
this.pitchTarget = target;
// enter code here for part (iv)(b)
}
/* instance methods */
/**
* Returns the receiver's taskNumber.
*/
public int getTaskNumber()
{
return this.taskNumber;
}
/**
* Returns the receiver's pitchAttempt.
*/
public double getPitchAttempt()
{
return this.pitchAttempt;
}
/**
* Returns the receiver's pitchError.
*/
public int getPitchError()
{
return this.pitchError;
}
/**
* Returns the receiver's pitchTarget.
*/
public int getPitchTarget()
{
return this.pitchTarget;
}
}