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



}

Recommended Answers

All 6 Replies

It seems you have a program that does some things correctly and other things incorrectly. You are reading in a CSV file and that is working fine? And you are trying to read data into a map and a list and that's sort of working, but not really? We don't have the input file and we don't have the ou package, so we can't compile or run this program.

I think you're probably going to have to slim down the program to something we can compile ourselves, and tell us exactly where and what the problem is, what works, what doesn't, etc. If there needs to be an input file, please provide one.

the ou import just lets me use a file chooser (normal windows style open a file from here type thingy!)

i've attached the two csv files...

if i use System.out.println(currentLine); after the 3 read in statements in my display pane it shows the correct data as read from the file and the data taken from the taskMap. it just wont put this data into the list object attempts...

hopefully you can help...

Well I can't compile it or run it without the ou package, but I don't really need to to see the problem:

Here's your TrialAdmin class data member.

private List<Attempt> attempts;

You instantiate it in the constructor:

attempts = new ArrayList<Attempt>();

Here's your local variable within a function.

Attempt attempts = new Attempt(taskNumber, pitchAttempt, pitchTarget);

To avoid confusion, name your local variable something different (replace line above with this line):

Attempt anattempt = new Attempt(taskNumber, pitchAttempt, pitchTarget);

Now just add your new Attempt object to your ArrayList of Attempt objects.

attempts.add (anattempt);

thank you for your speedy answers! it works a treat now!

does java often get confused then if you reuse variable names?

thank you for your speedy answers! it works a treat now!

You're welcome.

does java often get confused then if you reuse variable names?

Java doesn't get confused. I get confused (and I assume others get confused). attempts is a good name for a List and a bad name for a single Attempt object in my opinion. Here's an example of using the same name and keeping it straight. You can use the same name, but I generally don't if I can help it because I don't want to have to keep track. Use this for the global class data member.

public class Main
{
    int a;

    public Main ()
    {
        a = 5;
        dosomething ();
    }

    public void dosomething ()
    {
        int a = 4;
        System.out.println (a); // 4
        System.out.println (this.a); // 5
    }

    public static void main (String args[])
    {
        Main mainobj = new Main ();
    }
}

thanks. that makes sense now.

thanks again for the help! much appreciated!

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.