Search code examples
javajoptionpane

JOptionPane closes after input but should open second input dialog box


I am trying to create a code that asks the user to input 3 separate test scores. I am using the JOptionPane for dialog and input. After it asking for my first value it should open another and ask for my second value and so forth. Instead of doing so it closes and doesn't provide me with the second dialog.

Here is my code.

import java.util.Scanner;
import javax.swing.JOptionPane;

public class GradeTest
{
    public static void main(String[] args)
    {
        int Value1;
        int Value2;
        int Value3;
        int average;

        Scanner keyboard = new Scanner(System.in);

        JOptionPane.showInputDialog("What is your first value?");
        Value1 = keyboard.nextInt();

        JOptionPane.showInputDialog("What is your second value?");
        Value2 = keyboard.nextInt();

        JOptionPane.showInputDialog("What is your second value?");
        Value3 = keyboard.nextInt();

        average = Value1 + Value2 + Value3;
        average = average / 3;

        JOptionPane.showMessageDialog(null, "The average grade is " 
                                            + average + "%.");

        if(average < 60)
        {
            JOptionPane.showMessageDialog(null, "The grade for " 
                                                + average + "% is an F.");
        }
        else
        {
            if(average < 70)
            {
                JOptionPane.showMessageDialog(null, "The grade for " 
                                                    + average + "% is a D.");
            }
            else
            {
                if(average <80)
                {
                     JOptionPane.showMessageDialog(null, "The grade for " 
                                                        + average + "% is a C.");
                }
                else
                {
                    if(average <90)
                    {
                        JOptionPane.showMessageDialog(null, "The grade for " 
                                                            + average + "% is a B.");
                    }
                    else
                    {
                        if(average <100)
                        {
                            JOptionPane.showMessageDialog(null, "The grade for " 
                                                                + average + "% is an A.");
                        }
                    }
                }
            }
        }
    }
}

Solution

  • The default System.in Scanner is a command line input reader. If you set your values equal to the console input, you are doing absolutely nothing with the JOptionPane inputs. You should not mix console programs with GUI's generally. What you want to do is probably something more like this by using the JOptionPane results.

    ...
    int Value1, Value2, Value3, average;
    
    Value1 = Integer.parseInt(JOptionPane.showInputDialog("What is your first value?"));
    
    Value2 = Integer.parseInt(JOptionPane.showInputDialog("What is your second value?"));
    
    Value3 = Integer.parseInt(JOptionPane.showInputDialog("What is your third value?"));
    
    average = (Value1 + Value2 + Value3) / 3;
    
    JOptionPane.showMessageDialog(null, "The average grade is " + average + "%.");
    ...
    

    While this wasn't in your question, your if else clauses are extremely long and you should really shorten them to this.

    String avgGrade = "";
    if (average < 60)
        avgGrade = "F";
    else if (average < 70)
        avgGrade = "D";
    else if (average < 80)
        avgGrade = "C";
    else if (average < 90)
        avgGrade = "B";
    else
        avgGrade = "A";
    
    JOptionPane.showMessageDialog(null, "The grade for "+ average + "% is "+avgGrade+".");