Search code examples
javaswingjoptionpane

InputDialogue Joptionpane has -1


My program is working fine, but when it opens the JOptionPane.showInputDialog to enter a grade the text field for box has a highlighted -1 in it. I'm pretty clueless and searches came up with nothing. Thank you for your time!

enter image description here

import javax.swing.JOptionPane;

public class MeanDeviCalc extends javax.swing.JFrame {

//set array size
private double[] gradeArray = new double[25];
//intialize number of grades
private int GradeTotal = 0;

    /**
     * Creates new form MeanDeviCalc
     */
    public MeanDeviCalc() {
        initComponents();
    }

//get function for mean
public double getAverage(double[] gradeArray, int numElem) {
//intialize total with 0
    double total = 0;
    for (int i = 0; i < numElem; i++) 
    {
        //add one for total when grade inputed
        total=total+gradeArray[i];
    }
//divide for mean
    return (total/numElem);
}
//get function for standard deviation
public double getstddev(double[] gradeArray, int numElem, double average) {
//intialize total with 0
    double total = 0;
    for (int i = 0; i < numElem; i++) 
    {
        //standard deviation 
        total = total + Math.pow((gradeArray[i] - average), 2);
    }
    return Math.sqrt(total / numElem);
}

boolean exitloop = false;

do {
    String gradeInput = JOptionPane.showInputDialog(
            "Enter Grade",
            JOptionPane.PLAIN_MESSAGE);

    // When we receive empty/null input, we're done entering grades
    if (gradeInput == null || gradeInput.length() == 0) 
        exitloop=true;
    if(!exitloop){
    double gradeValue = 0; 

    if (GradeTotal == 25) {
        // max array size check
        JOptionPane.showMessageDialog(this,
        "You've already entered the maximum of 25 grades.",
        "Error",
        JOptionPane.ERROR_MESSAGE);
        return; 
    }

    try {
        gradeValue = Double.parseDouble(gradeInput);

    } catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(this,
        "Your input must be numeric!","Bad Data!",JOptionPane.ERROR_MESSAGE);

    }
    // Put grade in the array update total
    gradeArray[GradeTotal] = gradeValue;
    GradeTotal++;
    // Add to grade total
    txtGradeNumber.setText(Integer.toString(GradeTotal));

    double gradeAverage = getAverage(gradeArray, GradeTotal);
    txtMean.setText(Double.toString(gradeAverage));

    double standardDeviation = getstddev(gradeArray, GradeTotal, gradeAverage);
    txtStdDev.setText(Double.toString(standardDeviation));}
} while (GradeTotal < 25 && !exitloop) ;

Solution

  • Because you're using public static String showInputDialog(Component parentComponent, Object message, Object initialSelectionValue) and JOptionPane.PLAIN_MESSAGE is set to -1 (public static final int PLAIN_MESSAGE = -1;)

    I think you meant to use public static String showInputDialog(Component parentComponent, Object message, String title, int messageType)

    instead