-3

I'm trying to make this "Overall Grade Calculator" using javax.swing.*; that I recently learned. However, I can't find what is wrong with my code. My IDE, which is Ecliple, isn't detecting any error on my codes but it won't run when I try to run my codes. Where did I mess up?

BTW: This is by far my latest knowledge of Java Coding because I am self taught so I might not know any codes that are more advanced that these.

import javax.swing.*;

public class gradeCalcMk3 {

    public static double average(double a, double b, double c, double d) {
        double ave = a*0.3 + b*0.5 + c*0.1 + d*0.1;
        return ave;
    }

    public static void main(String[] args) {

        double grade[] = {0,0,0,0,0};

        JTextField name = new JTextField(10);
        JTextField q = new JTextField(3);
        JTextField ex = new JTextField(3);
        JTextField cs = new JTextField(3);
        JTextField ilm = new JTextField(3);

        JPanel myPanel = new JPanel();
        myPanel.add(new JLabel("Name:"));
        myPanel.add(name);
        myPanel.add(new JLabel("Q:"));
        myPanel.add(q);
        myPanel.add(new JLabel("Ex:"));
        myPanel.add(ex);
        myPanel.add(new JLabel("CS:"));
        myPanel.add(cs);
        myPanel.add(new JLabel("ILM:"));
        myPanel.add(ilm);

        grade[0] = Double.parseDouble(q.getText());
        grade[1] = Double.parseDouble(ex.getText());
        grade[2] = Double.parseDouble(cs.getText());
        grade[3] = Double.parseDouble(ilm.getText());
        grade[4] = average(grade[0], grade[1], grade[2], grade[3]);

        double confirm = JOptionPane.showConfirmDialog
                                (null, myPanel, "Enter Values", JOptionPane.OK_CANCEL_OPTION);
        if(confirm == JOptionPane.OK_OPTION) {
            JOptionPane.showMessageDialog(null, "Name: " + name.getText()
                                              + "\n\nQuiz: " + grade[0]
                                              + "\n\nExam: " + grade[1]
                                              + "\n\nCS: "   + grade[2]
                                              + "\n\nILM: "  + grade[3]
                                              + "Average: " + grade[4]);
        }
    }
}

Here's the output when I try to run it

Exception in thread "main" java.lang.NumberFormatException: empty String
    at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
    at sun.misc.FloatingDecimal.parseDouble(Unknown Source)
    at java.lang.Double.parseDouble(Unknown Source)
    at gradeCalcMk3.main(gradeCalcMk3.java:32)
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 4
    one of your TextFields is empty, and empty string cannot be parsed into a Double. – Michał Krzywański Apr 28 '19 at 07:12
  • shouldn't `double grade[]` be `{0.0d, 0.0d, 0.0d, 0.0d, 0.0d}`? – Shreyas Apr 28 '19 at 07:15
  • https://stackoverflow.com/questions/5499523/how-to-avoid-number-format-exception-in-java – Hasnain Ali Bohra Apr 28 '19 at 07:18
  • I was hoping that the window would appear and I would be able to enter my name and grade values. Then my grade values (which will be in strings) would be converted into double values. How do I do that? So that it won't say that the values are empty. – James Balles Apr 28 '19 at 07:21
  • @Shreyas not necessarily. up casting is automatic. besides, d is never needed. it's by default double. its for float that u need to suffix with f. – Ankit Apr 28 '19 at 07:32
  • While starting to learn coding, it's good practice to start reading and trying to understand the stacktrace. Exception, i.e. *Empty String* is on line 32 (grade[0] = Double.parseDouble(q.getText());) – Ankit Apr 28 '19 at 07:36
  • `double confirm = JOptionPane.showConfirmDialog` this opens a dialog which will wait until closing before performing any more code statements. It should be done ***before*** any calls to `getText`. – Andrew Thompson Apr 28 '19 at 10:20

3 Answers3

1

You will need a bit more to make this work.

Basically all of the code that extracts the values from the text field needs to be placed into a method that is triggered when you click on a button or menu (or something else to activate it). i.e. you need at least two methods, a "setup form" method and a "process the inputs" method.

As posted, your JTextFields contain no text (the constructor you are using does not set the text - it sets the "number of columns").

Once all of the text fields have been created and added to the form, the very next step is to extract the empty strings from them and attempt to parse them as doubles. This will generate the exception you encountered. Because the code is all contained in a single method, there is zero chance to allow you to enter any values into the fields before they are read and processed.

At the very least, comment out (for now) the code that attempts to extract values, parse them and compute the average. This extract, parse and calculate code can later be moved into the event handler attached to a button or menu that I mentioned above.

I hope this helps.

GMc
  • 1,764
  • 1
  • 8
  • 26
0

You're simply using the wrong constructor when creating instances of JTextField

JTextField(int columns) Constructs a new empty TextField with the specified number of columns.

Instead of providing an int, use a string, for example new JTextField("10")

JTextField(String text) Constructs a new TextField initialized with the specified text.

uminder
  • 23,831
  • 5
  • 37
  • 72
-1

Nvm, I found where I messed up and fixed it by doing this:

if(confirm == JOptionPane.OK_OPTION) {

            grade[0] = Double.parseDouble(q.getText());
            grade[1] = Double.parseDouble(ex.getText());
            grade[2] = Double.parseDouble(cs.getText());
            grade[3] = Double.parseDouble(ilm.getText());
            grade[4] = average(grade[0], grade[1], grade[2], grade[3]);

            JOptionPane.showMessageDialog(null, "Name: " + name.getText()
                                              + "\n\nQuiz: " + grade[0]
                                              + "\n\nExam: " + grade[1]
                                              + "\n\nCS: "   + grade[2]
                                              + "\n\nILM: "  + grade[3]
                                              + "Average: " + grade[4]);
        }
  • While that approach can work, you might want to consider the design pattern of adding buttons & text fields to a JFrame. While you can do some neat things with OptionPanes, true flexibility comes when you create a JFrame and add controls to it. Your app is driven by responding to user clicks (and other activities) in the controls. The big difference is that the former (option panes) are typically modal (i.e. you have to respond to it before you can do anything else and modeless (a typical GUI app). Granted there is a modeless mode for option panes but these are for "Search" style dialogs. – GMc Apr 28 '19 at 07:49