Java Array Problem PLEASE Help!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • SCOTT DELIBAC

    Java Array Problem PLEASE Help!

    Can Anyone Help??
    I have this program which is listed below. I want to take the grades that the students have assigned to them, and assign the letter grades a numeric value so that I can calculate their GPA. I just have no idea how I am supposed to do that?? Can anyone HELP!!!

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

    public class GradePoint
    {
    public static void main(String[] args)
    {

    String tempGrades;
    String[][] names = new String[10][6];
    String outputGrades = new String();
    float gpa;
    double A = 4;
    double a = 4;
    double B = 3;
    double b = 3;
    double C = 2;
    double c = 2;
    double D = 1;
    double d = 1;
    double F = 0;
    double f = 0;

    for(int x = 0; x < 3; x++)
    {
    names[x][0] = JOptionPane.sho wInputDialog(nu ll, "Please Enter Student number " + (x + 1) + "s Name ");
    for(int y = 1; y < 6; y++)
    {
    do
    tempGrades = JOptionPane.sho wInputDialog(nu ll, "Please Enter Grade for course number " + (y));
    while (!tempGrades.eq ualsIgnoreCase( "A") && !tempGrades.equ alsIgnoreCase(" B") && !tempGrades.equ alsIgnoreCase(" C") && !tempGrades.equ alsIgnoreCase(" D") && !tempGrades.equ alsIgnoreCase(" F"));
    names[x][y]=tempGrades;
    }

    }
    for(int x = 0; x < 3; x++)
    {
    outputGrades = "Students Name " + names[x][0] + "\n";
    for(int y = 1; y < 6; y++)
    outputGrades += "Grade for course number " + y + ": " + names[x][y] + "\n";
    JOptionPane.sho wMessageDialog( null, outputGrades);
    JOptionPane.sho wMessageDialog( null, names[x][y]);
    JOptionPane.sho wMessageDialog( null, "Grade Report Finished");
    System.exit(0);
    }

    }

  • Karl von Laudermann

    #2
    Re: Java Array Problem PLEASE Help!

    "SCOTT DELIBAC" <[email protected] phx.edu> wrote in message news:<XGkPb.270 44$Xq2.9730@fed 1read07>...[color=blue]
    > Can Anyone Help??
    > I have this program which is listed below. I want to take the grades
    > that the students have assigned to them, and assign the letter grades a
    > numeric value so that I can calculate their GPA. I just have no idea how
    > I am supposed to do that?? Can anyone HELP!!![/color]

    You can't access the variables A, a, B, b, etc. based on a string.
    Instead, store the grade values in a HashMap:

    HashMap grademap = new HashMap();
    grademap.put("A ", new Integer(4));
    grademap.put("B ", new Integer(3));
    grademap.put("C ", new Integer(2));
    grademap.put("D ", new Integer(1));
    grademap.put("F ", new Integer(0));

    Then you can look up the numeric value based on the grade string:
    int value = grademap.get(na mes[x][y].toUpperCase()) ;

    Comment

    • Karl von Laudermann

      #3
      Re: Java Array Problem PLEASE Help!

      "SCOTT DELIBAC" <[email protected] phx.edu> wrote in message news:<XGkPb.270 44$Xq2.9730@fed 1read07>...[color=blue]
      > Can Anyone Help??
      > I have this program which is listed below. I want to take the grades
      > that the students have assigned to them, and assign the letter grades a
      > numeric value so that I can calculate their GPA. I just have no idea how
      > I am supposed to do that?? Can anyone HELP!!![/color]

      You can't access the variables A, a, B, b, etc. based on a string.
      Instead, store the grade values in a HashMap:

      HashMap grademap = new HashMap();
      grademap.put("A ", new Integer(4));
      grademap.put("B ", new Integer(3));
      grademap.put("C ", new Integer(2));
      grademap.put("D ", new Integer(1));
      grademap.put("F ", new Integer(0));

      Then you can look up the numeric value based on the grade string:
      int value = grademap.get(na mes[x][y].toUpperCase()) ;

      Comment

      • SCOTT DELIBAC

        #4
        Re: Java Array Problem PLEASE Help!

        Thank you I got it.

        -Scott


        --
        Delibac
        "Karl von Laudermann" <[email protected] m> wrote in message
        news:e7e6125e.0 401210759.52501 [email protected] gle.com...[color=blue]
        > "SCOTT DELIBAC" <[email protected] phx.edu> wrote in message[/color]
        news:<XGkPb.270 44$Xq2.9730@fed 1read07>...[color=blue][color=green]
        > > Can Anyone Help??
        > > I have this program which is listed below. I want to take the grades
        > > that the students have assigned to them, and assign the letter grades a
        > > numeric value so that I can calculate their GPA. I just have no idea how
        > > I am supposed to do that?? Can anyone HELP!!![/color]
        >
        > You can't access the variables A, a, B, b, etc. based on a string.
        > Instead, store the grade values in a HashMap:
        >
        > HashMap grademap = new HashMap();
        > grademap.put("A ", new Integer(4));
        > grademap.put("B ", new Integer(3));
        > grademap.put("C ", new Integer(2));
        > grademap.put("D ", new Integer(1));
        > grademap.put("F ", new Integer(0));
        >
        > Then you can look up the numeric value based on the grade string:
        > int value = grademap.get(na mes[x][y].toUpperCase()) ;[/color]


        Comment

        • SCOTT DELIBAC

          #5
          Re: Java Array Problem PLEASE Help!

          Thank you I got it.

          -Scott


          --
          Delibac
          "Karl von Laudermann" <[email protected] m> wrote in message
          news:e7e6125e.0 401210759.52501 [email protected] gle.com...[color=blue]
          > "SCOTT DELIBAC" <[email protected] phx.edu> wrote in message[/color]
          news:<XGkPb.270 44$Xq2.9730@fed 1read07>...[color=blue][color=green]
          > > Can Anyone Help??
          > > I have this program which is listed below. I want to take the grades
          > > that the students have assigned to them, and assign the letter grades a
          > > numeric value so that I can calculate their GPA. I just have no idea how
          > > I am supposed to do that?? Can anyone HELP!!![/color]
          >
          > You can't access the variables A, a, B, b, etc. based on a string.
          > Instead, store the grade values in a HashMap:
          >
          > HashMap grademap = new HashMap();
          > grademap.put("A ", new Integer(4));
          > grademap.put("B ", new Integer(3));
          > grademap.put("C ", new Integer(2));
          > grademap.put("D ", new Integer(1));
          > grademap.put("F ", new Integer(0));
          >
          > Then you can look up the numeric value based on the grade string:
          > int value = grademap.get(na mes[x][y].toUpperCase()) ;[/color]


          Comment

          Working...