5 Replies - 5491 Views - Last Post: 14 May 2010 - 10:56 PM Rate Topic: -----

#1 Guest_Shane*


Reputation:

Adding record to a data file

Posted 11 May 2010 - 11:31 AM

Hi there,

I'm trying to add records to a .java file, I have three files, Grading.java, StudentRec.java, and students.dat.

I have the Grading.java and students.dat files all done, but I can't figure what to put into the StudentRec.java, it needs to be filled with the records so I can then print it later.

Grading.java:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.*;                     // package for StringTokenizer

public class Grading
{
   private static final int MAX_CLASS = 20;

   public static void main(String[] args) throws IOException
   {
      StudentRec[] gradeBook = new StudentRec[MAX_CLASS];  // gradebook
      int n = 0;                        // # of students in roster

      InputStreamReader reader = new
                InputStreamReader(System.in);
      BufferedReader console = new BufferedReader(reader);

      String s = console.readLine();
      while (s != null)                 // reading records from file
      {
        StringTokenizer sTk = new StringTokenizer(s);
        String fName = sTk.nextToken();
        String sName = sTk.nextToken();
        int hw = Integer.parseInt(sTk.nextToken());
        int ex1 = Integer.parseInt(sTk.nextToken());
        int ex2 = Integer.parseInt(sTk.nextToken());
        int ex3 = Integer.parseInt(sTk.nextToken());
        StudentRec studentRec = new 
                   StudentRec(fName, sName, hw, ex1, ex2, ex3);
        gradeBook[n] = studentRec;      // adding record to the gradebook
        n++;
        s = console.readLine();
      }

      int maxName = 0;                        // length of longest name
      int[] minScores = new int[4];           // min scores 
      int[] maxScores = new int[4];           // max scores
      int[] aveScores = new int[4];           // average scores
      for (int i=0; i<4; i++)
      {
         maxScores[i] = aveScores[i] = 0;     // initialization of arrays
         minScores[i] = 100;
      }
      for (int i=0; i<n; i++)                 // processing of records
      {
         if (maxName < gradeBook[i].getName().length()) // finding the longest
             maxName = gradeBook[i].getName().length(); //   name

         for (int j=0; j<4; j++)                        // computing min, max
         {                                              // and ave scores
            if (minScores[j] > gradeBook[i].getScore(j))
              minScores[j] = gradeBook[i].getScore(j);
            if (maxScores[j] < gradeBook[i].getScore(j))
              maxScores[j] = gradeBook[i].getScore(j);
            aveScores[j] += gradeBook[i].getScore(j);
         }
      }

//************************PRINTING THE ROSTER***********************
      for (int i=0; i<n; i++)              // loop over all student records
      {
         System.out.print(gradeBook[i].getName()+ " "); // print the name 
         for (int j=gradeBook[i].getName().length();    // name formatting
                  j <= maxName; j++)
           System.out.print(" ");

         for (int j=0; j<4; j++)                        // print the scores
           System.out.print(format(gradeBook[i].getScore(j))+" ");
         System.out.println("  " + format(gradeBook[i].getAverage()) + "   "
              + gradeBook[i].getGrade());
      }
     
//************************PRINTING THE STAT*************************     
      System.out.print("\nMin. scores:");
      for (int i=0; i<maxName-10; i++)                  // print spaces
         System.out.print(" "); 
      for (int j=0; j<4; j++)                           // min scores
         System.out.print(format(minScores[j])+" ");
      System.out.println("");     

      System.out.print("Ave. scores:");
      for (int i=0; i<maxName-10; i++)                  // print spaces
         System.out.print(" ");
      for (int j=0; j<4; j++)                           // average scores
         System.out.print(format((int)(Math.round(aveScores[j]/(n*1.0))))+" ");
      System.out.println("");

      System.out.print("Max. scores:");
      for (int i=0; i<maxName-10; i++)                  // print spaces
         System.out.print(" ");
      for (int j=0; j<4; j++)                           // max scores
         System.out.print(format(maxScores[j])+" ");
      System.out.println("");
   }

   public static String format(int k)                   // format a number
   {                                                    // for a nice output
      if (k < 10) return ("  " + k);
      else if (k<100) return (" " + k);
      else return ("" + k);
   }
}


students.dat:

Doe Joe 8 91 89 90
Brown Jane 89 81 78 95
Hopkins Michael 56 78 94 76
Smith Erin 95 85 84 79
Brown Jerry 91 87 79 100
Green Erin 88 92 67 89
Griggs Mary 78 0 97 82
Hopkins John 98 45 89 99
Peters Brian 64 90 100 85
Smith Molly 99 100 100 92

What do I put in the StudentRec.java?

This post has been edited by pbl: 14 May 2010 - 10:54 PM


Is This A Good Question/Topic? 0

Replies To: Adding record to a data file

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10488
  • View blog
  • Posts: 38,868
  • Joined: 27-December 08

Re: Adding record to a data file

Posted 11 May 2010 - 11:49 AM

Take a look at the FileWriter class. It allows you to append data to a File, so you don't have to read and re-write everything.
Was This Post Helpful? 1
  • +
  • -

#3 Ember  Icon User is offline

  • D.I.C Head

Reputation: 70
  • View blog
  • Posts: 160
  • Joined: 24-April 10

Re: Adding record to a data file

Posted 11 May 2010 - 12:33 PM

And to answer your question, based on the constructor you provided here:

StudentRec studentRec = new 
                   StudentRec(fName, sName, hw, ex1, ex2, ex3);



It looks like you want to set up the variables accordingly:
public class studentRec{
  private String firstName, surName;
  private int homework, exam1, exam2, exam3;

  public studenRec(String fName, String sName, int hw, int ex1, int ex2, int ex3){
    //Set data accordingly
   }

//Put in some Accessor methods as well as some Set methods
//like setXX(variable x){}  getXX(){  }  
}



Basically, check the Grade.java and any method used by studentRec, you have to figure out what that method is trying to obtain and implement it.

Just look for code like:
gradeBook[i].getName()



And your thinking process should be like: "I already input the name when I created the studentRec, now how do I grab those 2 Strings firstName and sName and how do I put them in the method getName()?"

and the answer should come very easily. If not, we are here to help :)

This post has been edited by Ember: 11 May 2010 - 12:39 PM

Was This Post Helpful? 0
  • +
  • -

#4 Guest_Shane*


Reputation:

Re: Adding record to a data file

Posted 14 May 2010 - 06:05 PM

So like this:

public class studentRec 
{
  private String firstName, surName;
  private int homework, exam1, exam2, exam3;

  public studenRec(String fName, String sName, int hw, int ex1, int ex2, int ex3)
  {
	firstName = fName;
	lastName = sName;
	this.hw = hw;
	this.ex1 = ex1;
	this.ex2 = ex2;
	this.ex3 = ex3;
  }

  public String getName()
  {
	return lastName + ", " + firstName;
  }
}

Was This Post Helpful? 0

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10488
  • View blog
  • Posts: 38,868
  • Joined: 27-December 08

Re: Adding record to a data file

Posted 14 May 2010 - 07:46 PM

That looks good to me for your getName() method. Keep in mind, though, that you will need getter and setter methods for all private variables you want to access or modify.

That looks good to me for a getName() method. Keep in mind, however, that you will also need getter and setter methods for all private (instance) variables that you want to access or modify, as private variables are not accessible outside of the class they are declared in.

This post has been edited by macosxnerd101: 14 May 2010 - 07:47 PM

Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Adding record to a data file

Posted 14 May 2010 - 10:56 PM

You are adding data to a data file not to a .java file
Your are not wrinting a Java code generator
Really misleading topic title renamed
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1