I am to have the user input a temperature and have the computer output which season it (probably) is based on the temperature entered. I have written this much and when I run the program, it seems to have the user enter the temperature in an input box and then won't return the probable season until the user enters it in the bottom of the program. I'm using JGrasp. I've been trying diffferent things but can't seem to come up with a solution. Any ideas?

Also, I'm having problems with the loop too.

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


public class TEMPERATUREHW1016

   public static void main(String [] args)
   {

      int temp;
      String runAgain;
      Scanner input = new Scanner(System.in);

      //do 
      {
         JOptionPane.showInputDialog(null,"Enter Temperature: ");
         temp = input.nextInt();

         findSeason(temp);
         runAgain = input.next();
      } 
      while((runAgain.equalsIgnoreCase("y"))); 
   }

   public static void findSeason(int temp)
   {
      if (temp > 110 || temp < -5)
         JOptionPane.showMessageDialog(null,"The temperature entered is outside the valid range.");
      else if (temp >= 90)
         JOptionPane.showMessageDialog(null,"It is probably summer."); 
      else if (temp >= 70 && temp < 90)
         JOptionPane.showMessageDialog(null,"It is probably spring.");
      else if (temp >= 50 && temp < 70)
         JOptionPane.showMessageDialog(null,"It is probably fall.");
      else if (temp < 50)
         JOptionPane.showMessageDialog(null,"It is probably winter.");

   }
}

Dani AI

Generated

The core issue is mixing GUI input with console input and not using the value returned by the dialog. As pointed out, JOptionPane.showInputDialog returns a String — your code displays the dialog but then ignores its return and waits on Scanner, so the program appears to ask twice. Also check your braces and loop: the sample posted looks like the do was commented out and the class opening brace is missing, which will cause compile errors.

Fix approach (quick checklist)

  • Use the String returned from showInputDialog and parse it to an int (handle NumberFormatException).
  • Don’t mix Scanner(System.in) and JOptionPane for the same interaction. Pick one UI style.
  • Let the user cancel (null return) and handle that gracefully.
  • For “run again” use JOptionPane.showConfirmDialog or another dialog instead of console input.
  • Verify braces and remove stray semicolons after while(...).

Example (minimal, GUI-only pattern)

import javax.swing.JOptionPane;

public class TemperatureSeasonFix {
    public static void main(String[] args) {
        while (true) {
            String input = JOptionPane.showInputDialog(null, "Enter temperature (F):");
            if (input == null) break; // cancel
            int temp;
            try {
                temp = Integer.parseInt(input.trim());
            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(null, "Please enter a whole number.");
                continue;
            }
            showSeason(temp);
            int again = JOptionPane.showConfirmDialog(null, "Try another temperature?", "Continue",
                                                      JOptionPane.YES_NO_OPTION);
            if (again != JOptionPane.YES_OPTION) break;
        }
    }

    static void showSeason(int t) {
        if (t < -5 || t > 110) {
            JOptionPane.showMessageDialog(null, "Temperature outside valid range.");
            return;
        }
        String season = (t >= 90) ? "probably summer"
                        : (t >= 70) ? "probably spring"
                        : (t >= 50) ? "probably fall"
                        : "probably winter";
        JOptionPane.showMessageDialog(null, "It is " + season + ".");
    }
}

Quick debugging tips: run the program from JGrasp, trigger an invalid input to see the exception message, and step through with a debugger or temporary System.out.println calls if dialogs disappear. This keeps the interaction purely GUI and avoids the double-input behavior you saw.

JOptionPane.showInputDialog(null,"Enter Temperature: ");
temp = input.nextInt();

You display the input dialog, but you do nothing with the result that it returns you.
You then go back to the console to read an int.
So the user enters something in the dialog, you ignore that, then he has to enter it again on the console.
Check the API doc for JOptionPane.showInputDialog - see what it returns.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.