Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Quick question

843785Mar 4 2009 — edited Mar 4 2009
I'm writing a program for a class project which says.

Write a program that prompts a professor to input grades for five different courses for 10 students. Prompt the professor to enter one grade at a time using the prompt "Enter grade for student #1" and "Enter grade #1". Verify that the prefessor enters only A,B,C,D, or F. Use variables for the student numbers ( 1 through 10) and grade numbers (1 through 5). Also have your program calculate the grade point average (GPA) for each student. A student receives four grade points for an A, three grade points for a B, two grade points for a C, one grade point for a D, and zero grade points for an F.

This is what I have so far - but for some reason - it only adds 4.0 once at this line. I used a Test set of A A A :
stuGPA[x] += 4.0; // NOT ADDING 4????

I put the system.out.println lines there to test the program - but it doesn't work. After the first A stuGPA[x] is 4 but after the second A it is still A - what is happening?
import javax.swing.JOptionPane;
import java.lang.*;

public class RecurProject
{   String[][] student = new String[10][5];
    String[] grades = new String[5];
    int numOfStudents = 10;
    int numOfStuGrades = 5;
    double[] stuGPA = new double[5];
    public RecurProject()
    {   for (int x = 0; x < numOfStudents; x++)
        {   for (int y = 0; y < numOfStuGrades; y++)    
            {   student[x][y] = JOptionPane.showInputDialog ("Enter grade #" + (y + 1) + " for student #" + (x + 1));
                if (student[x][y].equalsIgnoreCase("A"))
                {   System.out.println(stuGPA[x] + "stu GPA before adding 4");
                    stuGPA[x] += 4.0;       // NOT ADDING 4????
                    stuGPA[x] /= (y + 1);
                    System.out.println(stuGPA[x] + "stu GPA after adding 4");
                    System.out.println(y + "y + 1");
                    System.out.println(x);
                }
                else if (student[x][y].equalsIgnoreCase("B"))
                {   stuGPA[x] = (stuGPA[x] + 3) / (y + 1);
                }
                else if (student[x][y].equalsIgnoreCase("C"))
                {   stuGPA[x] = (stuGPA[x] + 2) / (y + 1);
                }
                else if (student[x][y].equalsIgnoreCase("D"))
                {   stuGPA[x] = (stuGPA[x] + 1) / (y + 1);
                }
                else
                {   student[x][y] = "F";
                    JOptionPane.showMessageDialog(null, "You may have entered unacceptable input - it has been received as an F.");
                    stuGPA[x] = (stuGPA[x] + 0) / (y + 1);
                }
            }
        }
    }
}
I wrote a test program to test if i could add stuff to a array variable and it works just fine:
public class test
{   
    int[] tester = new int[10];
    public void tester()
    {   for (int x = 0; x < 5; x++)
        {   tester[0] += 4;
            System.out.println(tester[0]);
        }
    }
}
Edited by: Pluberus on Mar 4, 2009 7:52 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 1 2009
Added on Mar 4 2009
8 comments
186 views