Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Confused on why code won't work

I'm fairly new to the java language but I can't seem to get my code to work. It is supposed to read an input file with names and 10 quiz scores for each person, then do a bubble sort until we have the top 8 scores, then create an average of those scores and give it a letter grade. The final output for each person is supposed to look like this:

Name: blah - Grade: B

The input file looks like this

Bill Brasky
81
82
98
74
76
91
88
82
92
87
next name
and so on

Here's what my code looks like:

[code]
import java.io.*;

public class StudentGrades
{
public static void main(String args[]) throws Exception
{
String LETTER;
final int SIZE = 10; // Number of quizzes
double score[] = new double[SIZE]; // Array used to store 10 quiz scores.
String stuName; // Used to store name.
int x;
x = 0;
final int LIMIT = SIZE;
String stuScoreString; // used to store what is read before changing to integer.
int stuScore = 0;
int pairsToCompare;
double gradeAvg;
gradeAvg = 0;
boolean switchOccurred;
boolean done;
double temp;
double totalGrade;
totalGrade = 0;
int y;
y = 0;
System.out.println("Student Grade Report by Russ Penning");

// Start the input read.
FileReader fr = new FileReader("scores.dat");
BufferedReader br = new BufferedReader(fr);

// This is the work done in the fillArray() method
while((stuName = br.readLine()) != null) // Use to read only the first line of the array before change to integer.
{
x = 0;
done = false;
while(x < LIMIT)
{
// Place value in array.
stuScoreString = br.readLine();
// Convert String to integer.
stuScore = Integer.parseInt(stuScoreString);
score[x] = stuScore;
x++; // Get ready for next input item.

} // End of input loop.
stuScore = x - 2;
pairsToCompare = stuScore - 1;

// This is the work done in the sortArray() method
switchOccurred = true; // set flag to true

while(switchOccurred == true)
{
x = 0;
switchOccurred = false;
while(x < pairsToCompare)
{
if(score[x] > score[x + 1])
{
temp = score[x + 1];
score[x+1] = score[x];
score[x] = temp;
switchOccurred = true;
}
x++;
}
pairsToCompare--;
}
while(x < stuScore)
{
totalGrade = (score[x+2] + totalGrade);
x++;
}
gradeAvg = totalGrade / 8;

// Call calculateGrades method here
LETTER = calculateGrades(gradeAvg);
}
x = 0;
while(x < y)
{
LETTER = "";
System.out.println("Name: " + stuName + " - Grade: " + LETTER);
x++;
}

br.close();
System.exit(0);

} // End of main() method.

// Write calculateGrades method here.
public static String calculateGrades(double gradeAvg)
{

String LETTER;

if(gradeAvg >= 90)
LETTER = "A";
else if (gradeAvg >= 80)
LETTER = "B";
else if (gradeAvg >= 70)
LETTER = "C";
else if (gradeAvg >= 60)
LETTER = "D";
else
LETTER = "F";

return LETTER;
}

} // End of StudentGrades class.
[/code]

If anyone has any idea what is wrong with my code... Please let me know.

Thanks, Russ

Comments

  • Josh CodeJosh Code Member Posts: 675
    Try this:

    [code]
    import java.io.*;

    public class StudentGrades
    {
    public static void main(String args[]) throws Exception
    {
    String LETTER;
    final int SIZE = 10; // Number of quizzes
    double score[] = new double[SIZE]; // Array used to store 10 quiz scores.
    String stuName; // Used to store name.
    final int LIMIT = SIZE;
    String stuScoreString; // used to store what is read before changing to integer.
    int stuScore = 0;
    double gradeAvg;
    gradeAvg = 0;
    System.out.println("Student Grade Report by Russ Penning");

    // Start the input read.
    FileReader fr = new FileReader("scores.dat");
    BufferedReader br = new BufferedReader(fr);

    // This is the work done in the fillArray() method
    while((stuName = br.readLine()) != null) // Use to read only the first line of the array before change to integer.
    {
    for (int x=0;x < LIMIT;x++)
    {
    // Place value in array.
    stuScoreString = br.readLine();
    // Convert String to integer.
    stuScore = Integer.parseInt(stuScoreString);
    score[x] = stuScore;

    } // End of input loop.
    sortScores(score);
    gradeAvg = calcAverage(score);

    // Call calculateGrades method here
    LETTER = calculateGrades(gradeAvg);

    System.out.println("Name: " + stuName + " - Grade: " + LETTER);
    }

    br.close();

    } // End of main() method.

    public static double calcAverage(double [] scores)
    {
    double total = 0;

    for (int x=0;x score[x + 1])
    {
    double temp = score[x + 1];
    score[x+1] = score[x];
    score[x] = temp;
    switchOccurred = true;
    }
    }
    }

    }

    // Write calculateGrades method here.
    public static String calculateGrades(double gradeAvg)
    {

    String LETTER;

    if(gradeAvg >= 90)
    LETTER = "A";
    else if (gradeAvg >= 80)
    LETTER = "B";
    else if (gradeAvg >= 70)
    LETTER = "C";
    else if (gradeAvg >= 60)
    LETTER = "D";
    else
    LETTER = "F";

    return LETTER;
    }

    } // End of StudentGrades class.
    [/code]
  • Josh CodeJosh Code Member Posts: 675
    I just noticed that you want the top 8 average instead of average of all so here is the corrected version:

    [code]
    import java.io.*;

    public class StudentGrades
    {
    public static void main(String args[]) throws Exception
    {
    String LETTER;
    final int SIZE = 10; // Number of quizzes
    double score[] = new double[SIZE]; // Array used to store 10 quiz scores.
    String stuName; // Used to store name.
    final int LIMIT = SIZE;
    String stuScoreString; // used to store what is read before changing to integer.
    int stuScore = 0;
    double gradeAvg;
    gradeAvg = 0;
    System.out.println("Student Grade Report by Russ Penning");

    // Start the input read.
    FileReader fr = new FileReader("scores.dat");
    BufferedReader br = new BufferedReader(fr);

    // This is the work done in the fillArray() method
    while((stuName = br.readLine()) != null) // Use to read only the first line of the array before change to integer.
    {
    for (int x=0;x < LIMIT;x++)
    {
    // Place value in array.
    stuScoreString = br.readLine();
    // Convert String to integer.
    stuScore = Integer.parseInt(stuScoreString);
    score[x] = stuScore;

    } // End of input loop.
    gradeAvg = calcAverageOfTop8(score);

    // Call calculateGrades method here
    LETTER = calculateGrades(gradeAvg);

    System.out.println("Name: " + stuName + " - Grade: " + LETTER);
    }

    br.close();

    } // End of main() method.

    public static double calcAverageOfTop8(double [] scores)
    {
    sortScores(scores);
    double top8Total=0;

    for (int x=scores.length-8;x score[x + 1])
    {
    double temp = score[x + 1];
    score[x+1] = score[x];
    score[x] = temp;
    switchOccurred = true;
    }
    }
    }

    }

    // Write calculateGrades method here.
    public static String calculateGrades(double gradeAvg)
    {

    String LETTER;

    if(gradeAvg >= 90)
    LETTER = "A";
    else if (gradeAvg >= 80)
    LETTER = "B";
    else if (gradeAvg >= 70)
    LETTER = "C";
    else if (gradeAvg >= 60)
    LETTER = "D";
    else
    LETTER = "F";

    return LETTER;
    }

    } // End of StudentGrades class.
    [/code]
Sign In or Register to comment.