Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
TheDapperGinger
Sep 30, 2004

I wear national geographic jackets!
This is for a school project, I've been staring at it too long so its all blending together and I can't workout why this only outputs the last name in the text file. I'll be checking back regularly if you have questions or need the other class posted.
code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 * NEEDS TO WRITE TO FILE DOES NOT YET.
 */

package Problem2;

import java.io.*;
import java.util.StringTokenizer;
import javax.swing.*;



/**
 *
 * @author Austin
 */
public class GradeDriver {
public static String strtemp;
public static String input;
public static String studentname;
public static String studentid;
public static Double dbltemp;
public static Double exam1scores;
public static Double exam2scores;
public static Double exam3scores;
public static Double exam4scores;
public static Double homeworkscores;
public static Double[] averages;
public static char chartemp;
public static char[] lettergrades;
public static int i=0;
public static int linecount=0;
public static boolean swapped=false;
public static GradeClass[] gc;
public static GradeClass[] gctemp = new GradeClass[0];


    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileNotFoundException, IOException {

        BufferedWriter bw = new BufferedWriter(new FileWriter("gradereport.txt"));
       BufferedReader br = new BufferedReader(new FileReader("grades.txt"));
      
       strtemp=br.readLine();
       strtemp=br.readLine();
       while(br.ready())
       {
        strtemp=br.readLine();
        linecount+=1;
       }
       br.close();
       //initialize the variables to the linecount of the file sans the headings
       
       averages = new Double[linecount];
       lettergrades = new char[linecount];
       gc = new GradeClass[linecount];
       br = new BufferedReader(new FileReader("grades.txt"));

       strtemp=br.readLine();
       strtemp=br.readLine();
       
       
       

       while(br.ready())
       {
        int incrementer=0;
       input=br.readLine();
       StringTokenizer st = new StringTokenizer(input,",");
       studentname=st.nextToken();
       
       studentid=st.nextToken();
       
       exam1scores=Double.parseDouble(st.nextToken());
       exam2scores=Double.parseDouble(st.nextToken());
       exam3scores=Double.parseDouble(st.nextToken());
       exam4scores=Double.parseDouble(st.nextToken());
       homeworkscores=Double.parseDouble(st.nextToken());

       //======================================================================
       
       gc[i]= new GradeClass(studentname,studentid,exam1scores,
       exam2scores,exam3scores,exam4scores,homeworkscores);
       
      System.out.println(gc[i].getname());
       averages[i]=gc[i].Gradecalc();
       lettergrades[i]=gc[i].calclettergrade();


       //======================================================================
       i+=1;

       }
       br.close();
       JTextArea outputarea = new JTextArea(20,50);

/*
int n = gc.length;
    for (int pass=1; pass < n; pass++) {
        for (int z=0; z < n-pass; z++) {
            if ((gc[z].getname()).compareToIgnoreCase(gc[z+1].getname())<0)
            {
                System.out.println(gc[z]);
                // exchange elements
                gctemp[0] = gc[z];
                gc[z] = gc[z+1];
                gc[z+1] = gctemp[0];
            }
        }
    }

/*
    for (int pass=1; pass < gc.length; pass++) {  // count how many times
        // This next loop becomes shorter and shorter
        for (i=0; i < gc.length-pass; i++) {
            if ((gc[i].getname()).compareToIgnoreCase(gc[i+1].getname())>0)
            {
               
    gctemp[0]=gc[i];
    gc[i]=gc[i+1];
    gc[i+1]=gctemp[0];

            }
        }
    }



 */
outputarea.setEditable(false);

i=0;
for(i=0;i<gc.length;i++)
{

outputarea.append("Grade Report for: " + gc[0].getname()+ "\n"
        +"=======================================\n"
        +"=  Student ID:  " + gc[i].getid() + "\n"
        +"=           GPA: " + Math.ceil(averages[i]) + "\n"
        +"=Letter Grade: " + lettergrades[i] + "\n");

}
System.out.println(gc[i].getname());
 FileWriter fstream = new FileWriter("out.txt");
        BufferedWriter out = new BufferedWriter(fstream);
    
    //Close the output stream
JScrollPane soutputarea = new JScrollPane(outputarea);
       JOptionPane.showMessageDialog(null,soutputarea);


} 

Adbot
ADBOT LOVES YOU

TheDapperGinger
Sep 30, 2004

I wear national geographic jackets!

rjmccall posted:

Meditate upon this passage, young one.

I'm not sure I understand what you mean. What's wrong with that snippet. What I was trying to do was pre-count the number of data lines read from a text file with 2 header lines.

TheDapperGinger
Sep 30, 2004

I wear national geographic jackets!

rjmccall posted:

That'll teach me not to actually read your code — although, why are you actually doing this? What about this problem requires you to read the entire file first?

Anyway, look at the code that prints out reports, and tell me why it always prints the name of the first student (i.e. the student at index 0).

That one is my bad, the 0 in that case is left in while i was testing. In the file we were given the program always prints the last name in the file. It should have read gc[i] just like the rest of the output block. I should have commented it out before posting it, and I should have used pastebin.

[edit]also I should mention that we are reading data from a csv and populating an array of a class for student grades which is why I had to cheat and count the lines first. [/edit]

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply