How can I stop an empty variable from being output?

Hip :

The following program should show up a window where it is asking to type in a number from 1 to 12. Each number is set for being a value of a month(1 = January, etc.). Then it should output the quarter of the year where the typed in a month is located in(1 = January; January = 1. Quarter of the year). A wrong input number should output an error message to the user. The evaluation is made with Switch Case.

Almost everything is working so far, but the problem I have is that when I put in a number out of range(1 to12), that means smaller than 1 or larger than 12, I first get the output message "Wrong input!" and then also the message "Quarter: ".

I have checked oracle website for that issue, but I couldn't find any information on this. They have a similar code but it outputs in the command line and I want to do that with JOptionPane.showMessageDialog

import javax.swing.*;

public class WhichQuarterIsThis
{
    public static void main(String[] args)
    {
        String input, output, quarter;
        int inputNumber;

        input = JOptionPane.showInputDialog(null, "put in a number (1-12).");
        inputNumber = Integer.parseInt(input);
        quarter = "";

        switch(inputNumber)
        {
            case 1:
            case 2:
            case 3: quarter = "1";
                break;
            case 4:
            case 5:
            case 6: quarter = "2";
                break;
            case 7:
            case 8:
            case 9: quarter ="3";
                break;
            case 10:
            case 11:
            case 12: quarter = "4";
                break;

            default: JOptionPane.showMessageDialog(null,"wrong input!");
        }
        if(inputNumber >= 1 || inputNumber <= 12)
        {
            JOptionPane.showMessageDialog(null, "Quarter: " + quarter);
        }
    }
}

"Quarter: " field should not be output when the input value is out of that range between 1 and 12.

Thanks in advance.

Madplay :

Modify the conditional statement as follows.

if(inputNumber >= 1 && inputNumber <= 12)
{
    JOptionPane.showMessageDialog(null, "Quarter: " + quarter);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=149598&siteId=1