Hi i am doing a student grade lab where the student enters a grade they got on a test and they get a letter grade. My program only gives the user a F even if it should be another grade. This is my code.

strT1 = JOptionPane.showInputDialog("Enter 1st test score:");
        std.test1 = Integer.parseInt(strT1);
        Te1.setText("Your grade for test 1 is " + std.T1);

static char letterGrade(double g) {
   if (g >= 90)
      return 'A';   // 90 or above gets an A
   else if (g >= 80)
      return 'B';   // 80 to 89 gets a B
   else if (g >= 65)
      return 'C';   // 65 to 79 gets a C
   else if (g >= 50)
      return 'D';   // 50 to 64 gets a D
   else
      return 'F';   // anything else gets an F
}
   public char T1 = letterGrade (test1);

Thanks for the help.

Edited 4 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

ow key, I'm not entirely sure here ...
is this code all within one class, are it two classes ...
can you post the entire code of your application, including the complete main method?

I think the problem is that you call letterGrade before test1 was assigned a value, and still has the default value 0.

This is two classes the first class is

public class Layout extends javax.swing.JFrame {
    public Layout() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        Te1 = new javax.swing.JLabel();
        Te3 = new javax.swing.JLabel();
        Te2 = new javax.swing.JLabel();
        Avg1 = new javax.swing.JLabel();
        name1 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(51, 255, 0));

        jButton1.setText("Calculate");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        Te1.setText("Test score 1");
        Te1.addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
                Te1AncestorMoved(evt);
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });

        Te3.setText("Test score 3");

        Te2.setText("Test score 2");

        Avg1.setText("Average");

        name1.setText("Name");

        jLabel1.setText("A 100-90");

        jLabel2.setText("B 89-80");

        jLabel3.setText("C 79-70");

        jLabel4.setText("D 69-60");

        jLabel5.setText("F 59-0");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addContainerGap(464, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(Avg1, javax.swing.GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE)
                        .addGap(300, 300, 300))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addComponent(Te3, javax.swing.GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE)
                                .addGap(124, 124, 124))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addComponent(Te2, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE)
                                .addGap(125, 125, 125))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addComponent(Te1, javax.swing.GroupLayout.DEFAULT_SIZE, 231, Short.MAX_VALUE)
                                .addGap(90, 90, 90)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jLabel3)
                                        .addComponent(jLabel2)
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel5)
                                            .addComponent(jLabel4)))))
                            .addComponent(name1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(176, 176, 176))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(name1, javax.swing.GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(Te1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(Te2, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(Te3, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(Avg1, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton1))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel5)))
                .addGap(38, 38, 38))
        );

        jMenu1.setText("File");

        jMenuItem1.setText("Clear");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuItem4.setText("Exit");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem4);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Help");

        jMenuItem3.setText("Instrucations");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem3);

        jMenuItem2.setText("About");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem2);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
    // resets the labels 
    name1.setText("Name");
    Te1.setText("Test score 1");
    Te2.setText("Test score 2");
    Te3.setText("Test score 3");
    Avg1.setText("Average");
    }                                          

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
     // declalare varables
     student std;
     String strT1, strT2, strT3;
     std = new student();
//------------------------------------------------------------------------------
   //collects the student name
   std.name = JOptionPane.showInputDialog("Enter student name:");
     //outputs student names
     name1.setText("Name " + std.name );
//------------------------------------------------------------------------------
    strT1 = JOptionPane.showInputDialog("Enter 1st test score:");
        std.test1 = Integer.parseInt(strT1);
        Te1.setText("Your grade for test 1 is " + std.T1);
//------------------------------------------------------------------------------        
    strT2 = JOptionPane.showInputDialog("Enter 2nd test score:");
        std.test2 = Integer.parseInt(strT2);
        Te2.setText("Your grade for test is " + std.T2 );
//------------------------------------------------------------------------------ 
    strT3 = JOptionPane.showInputDialog("Enter 3rd test score:");
        std.test3 = Integer.parseInt(strT3);
        Te3.setText("Your grade for test is " + std.T3  );
//------------------------------------------------------------------------------         
    Double avg = std.getAverage();
        JOptionPane.showMessageDialog(null, "Your average is "  +  std.getAverage() );
        Avg1.setText("Your Average is " + std.A1 );
    }                                        

    private void Te1AncestorMoved(javax.swing.event.AncestorEvent evt) {                                  
    
    }                                 

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
        // declalare varables
        about Aframe = new about(); 
        Layout zframe = new Layout();
        //set the frame visible
        Aframe.setVisible(true);
        // set the frame invisible
        zframe.setVisible(false);
    }

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {
        // declalare varables
        dir Aframe = new dir();  
        Layout zframe = new Layout();
        //set the frame visible
        Aframe.setVisible(true);
        // set the frame invisible
        zframe.setVisible(false);
    }

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {
        // exit the program
        System.exit(WIDTH);
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Layout().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JLabel Avg1;
    private javax.swing.JLabel Te1;
    private javax.swing.JLabel Te2;
    private javax.swing.JLabel Te3;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel name1;
    // End of variables declaration
}

The other class is

public class student {
  // declalare varables
    Layout std2;
    public String name; // Student’s name.
    public int test1, test2, test3;  //Grades on thre tests
    //------------------------------------------------------------------------------ 
static char letterGrade(double g) {
   if (g >= 90)
      return 'A';   // 90 or above gets an A
   else if (g >= 80)
      return 'B';   // 80 to 89 gets a B
   else if (g >= 65)
      return 'C';   // 65 to 79 gets a C
   else if (g >= 50)
      return 'D';   // 50 to 64 gets a D
   else
      return 'F';   // anything else gets an F
}
   public char T1 = letterGrade (test1); 
//------------------------------------------------------------------------------
   static char letterGrade2(double b) {
   if (b >= 90)
      return 'A';   // 90 or above gets an A
   else if (b >= 80)
      return 'B';   // 80 to 89 gets a B
   else if (b >= 65)
      return 'C';   // 65 to 79 gets a C
   else if (b >= 50)
      return 'D';   // 50 to 64 gets a D
   else
      return 'F';   // anything else gets an F
}
   public char T2 = letterGrade2 (test2); 
//------------------------------------------------------------------------------    
static char letterGrade3(double c) {
   if (c >= 90)
      return 'A';   // 90 or above gets an A
   else if (c >= 80)
      return 'B';   // 80 to 89 gets a B
   else if (c >= 65)
      return 'C';   // 65 to 79 gets a C
   else if (c >= 50)
      return 'D';   // 50 to 64 gets a D
   else
      return 'F';   // anything else gets an F
}
   public char T3 = letterGrade3 (test3);    
//------------------------------------------------------------------------------   
static char letterGrade4(double a) {
   if (a >= 90)
      return 'A';   // 90 or above gets an A
   else if (a >= 80)
      return 'B';   // 80 to 89 gets a B
   else if (a >= 65)
      return 'C';   // 65 to 79 gets a C
   else if (a >= 50)
      return 'D';   // 50 to 64 gets a D
   else
      return 'F';   // anything else gets an F
}
   public char A1 = letterGrade4 ((test1 + test2 + test3) / 3); 
//------------------------------------------------------------------------------    
   public double getAverage(){ // compute average test grade
return (test1 + test2 + test3) / 3;
}
}  // end of function letterGrade

 // end of class Student

Edited 4 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

well ...
your student class doesn't make much sense ..
you don't need three copies of that letterGrade method, you just need to call it with different values.
also, you never set the values of your test1, ... variables, so they have the default value 0, which makes it normal that you get the according letter as result.

would this line of code set a value for test
std.test1 = Integer.parseInt(strT1);
strT1 is the value the user puts in

since that variable is public ... yes.
a better approach would be to make those variables private and use setters and getters to get to the or change the value.

This question has already been answered. Start a new discussion instead.