user3511057
user3511057

Reputation: 97

Using BufferedReader and Scanner to read text

I am studying Java and using BlueJ so my question relates to part of an assignment I have been asked to do. Some of it works but I am having problems with other parts. I will include everything that I have done in the class so far but first here are the requirements:

  1. Prompt user to select an appropriate file // done and working
  2. Use BufferedReader and Scanner objects to read a file line by line // done and assume working!
  3. As each line containing the name and age of a runner is read, a new Runner object should be created and its instance variable set as follows: // Class Runner already created // Class MathatonAdmin - current class.

    • 3a) name - can be set directly using the value from the file. // tested with System.out.println and all values (in display panel) are shown (*).
    • 3b) ageGroup - can be worked out from the given age: runners under 18 should be categorised as junior, 55 and older as senior, all else as standard. // tested with System.out.println and all values (in display panel) are shown (*).
    • 3c) The instance of Runner should be added to the list referenced by the instance variable runners.

Essentially when I run the test code provided:

MarathonAdmin ma = new MarathonAdmin();
ma.readInRunners();

I am supposed t o see a list of runners when I inspect ma; currently one the name and age of a single person is listed.

So I need help with 3a - 3c. How do I create a new instance of Runner with said variables, then add the instance Runner to the list in runners?

I have tried a for loop in while loop but since I am guessing the for loop I do not get the required list in the variable ma.

I am using System.out.println for testing that I at lest have the correct file.

Any help or advice will be appreciated.

The class MarathonAdmin:

import java.util.*;
import java.io.*;
import ou.*;

/**
* MatharthonAdmin Class
* 
* @author Stephen Berry 
* @version 28/03/14
*/
public class MarathonAdmin
{
   // instance variables 
   private String runners;
   private String age;

   /**
   * Constructor for objects of class MarathonAdmin
   */
   public void MarathonAdmin()
   {
      List<String> runners = new ArrayList<>();
   }

   public void readInRunners()
   {
      String pathName = OUFileChooser.getFilename();
      File aFile = new File(pathName);
      BufferedReader bufferedFileReader = null;

      try 
      {
         String currentLine;
         Scanner lineScanner;
         bufferedFileReader = new BufferedReader(new FileReader(aFile));
         currentLine = bufferedFileReader.readLine();

      while (currentLine != null)
         {
            lineScanner = new Scanner(currentLine);
            lineScanner.useDelimiter(",");
            runners = lineScanner.next();
            age = lineScanner.next();

            for (String aList: runners)
            {
               Runner runners = new Runner();

               if (Integer.parseInt(age) < 18)
               {
                  System.out.println(currentLine + " : Junior");
               }

               if (Integer.parseInt(age) > 55)
               {
                  System.out.println(currentLine + " : Senior");
               }

               if (Integer.parseInt(age) > 18 && Integer.parseInt(age) < 55)
               {
                  System.out.println(currentLine + " : Standard");
               }

               currentLine = bufferedFileReader.readLine();
            }
         }
      }

      catch (Exception anException)
      {
         System.out.println("Error: " + anException);
      }

      finally
      {
         try
         {
            bufferedFileReader.close();
         }

         catch (Exception anException)
         {
            System.out.println("Error: " + anException);
         }
      }
   }

}

Upvotes: 2

Views: 1513

Answers (3)

NotsoCleverAlex
NotsoCleverAlex

Reputation: 23

Your instance variables are incorrect. You need a list of objects not a String runners. Also you need a method instance for age not a class instance.

private List<Runner> runners; // instance variables for a list of runner objects

runners = new ArrayList<Runner>(); // in the constructor

I would also agree with MeiSign, you do not need a for loop within the while loop

Upvotes: 0

MeiSign
MeiSign

Reputation: 1507

There are some points in your class that lead to this missbehaviour of your program.

  1. The Exercise says that you shall create a list of runner objects as instance variable right? Look at your instance variable runners of your marathon class and look at the type of it. ;)

  2. Your while loop is a good approach. Now inside the while loop you interate for each line of the textfile which is equivalent to one runner right? So why you need the for loop? You can use the lineScanner to get each part of the line there is no need for a second loop i try to give you a structure with pseudocode

String runnerName; Int runnerAge;

while (currentLine != null)
     {
        lineScanner = new Scanner(currentLine);
        lineScanner.useDelimiter(",");

        runnerName = lineScanner.next();
        runnerAge = lineScanner.next();

        runners = lineScanner.next();
        age = lineScanner.next();

        create new Runner Object

        set age and name of object according to the data you ve just read

        runners.add(Runnerobject) // <-- this line adds the object you ve just created to the list which is your instancevariable. Make sure that this variable really is a list :)

        currentLine = bufferedFileReader.readLine();
     }
    }

Hope this helps you a little bit.

Upvotes: 1

Gregor Koukkoullis
Gregor Koukkoullis

Reputation: 2305

You create a Runner instance, but you don't set the instance variables name and age/ageGroup for class Runner. You could create a constructor in the Runner class to do that. Also you should rename your variable, since runners is already used.

Further you declared runners as an instance variable of type String. But you would need to have a List. After you created the Runner instance you can add that instance to the list.

Upvotes: 0

Related Questions