I am working on an assignment for a class I'm taking. Currently, I have a while loop which takes data from the user through JOptionPane. I am suppose to use a sentinel value (q or Q) to tell the loop to stop. I have my if statement argument working, but I am not sure whether there is a method to turn off JOptionPane. What would be a good way to do this?

/*
Takes user grades until the user types Q.
This loop takes any integer value.
If the user types Q (or q), the loop
stops.
*/
while(!input.equalsIgnoreCase("Q"))
  {
      grader.addGrade(gradeInput);
      JOptionPane.showInputDialog(informativeMessage);	
 }
/*
If the user types Q (or q), the loop
stops. The dialog then disappears.
*/

if(input.equalsIgnoreCase("Q"))
   {
       //JOptionPane closes
          
   }

When you use an InputDialog the input is received when you click OK, which also closes it, until it is called again. To get out of the while loop you can 'break' it. I wrote an example to show you what I mean.

public class LeapYear {
	
 public static void main(String[] args) {
  //creates the year varible and the sentinel value
  int year, sentinel =8;
  //creates a gregorian calender to use the year variable with
  GregorianCalendar calender = new GregorianCalendar();
  //this while loop will run until sentinel = 0 or a break; is called
  while(sentinel != 0){
  // gets the year from the InputDialog
  year = Integer.parseInt(JOptionPane.showInputDialog("What is the year? (0 to quit)"));
        //ends while loop if they entered the quit value
	if(year == 0){
	 	break;
	}
        //makes sure the year is within the correct range
	if(year < 1582 || year > 2020){
	        JOptionPane.showMessageDialog(null, "Year out of range");
        }
        //confirm leap year
	else if(calender.isLeapYear(year)){
		JOptionPane.showMessageDialog(null, "The year you entered is a leap year.");
	}else{
	         JOptionPane.showMessageDialog(null, "Not a leap year");
	}
    }
}

}

The if statement on line 9 checks the input gotten from the first JOptionPane.showInputDialog. The only way the input exists, is if you click OK in the input dialog, which closes it. The break; in the if loop breaks the loop, ending it and in doing so, ending the program. If the while loop never ends it will keep opening an InputDialog, checking to see if it is a leap year, and than start all over again.

I tried your suggestion. It worked, but unfortunately it triggered a thread exception error. I just want the loop to stop after the user is done, then take the values and do what I want with them afterwards.


Exception in thread "main" java.lang.NumberFormatException: For input string: "q"
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at GradeAnalyzerTester.main(GradeAnalyzerTester.java:13)

However, when I type an integer (ex. 90), then the sentinel value (Q or q), the loop does not break. It's trapped in an infinite loop.

^ I ran my program through the Eclipse debugger. It seems that exception was caused because I typed Q first, but my program is expecting a decimal value (since the Input string is parsed to a double variable). However, when I type in values, the loop seems to run fine until I try to exit. When I look at the program in the debugger, it appears to ignore that I typed Q or q. I'm not sure why this is happening.

You can first check if the input is 'Q' or 'q' and then try to convert it into a double

/*
Takes user grades until the user types Q.
This loop takes any integer value.
If the user types Q (or q), the loop
stops.
*/
while(!input.equalsIgnoreCase("Q"))
  {
      grader.addGrade(gradeInput);
      JOptionPane.showInputDialog(informativeMessage);	
 }

if I'm not mistaken, which does occurs :) you have everything you need just in this part, but you forget one very important thing:
you don't change the value of input, so input will never get the value inputted by the inputbox. maybe try this:

while(!input.equalsIgnoreCase("Q"))
  {
      grader.addGrade(gradeInput);
     input = JOptionPane.showInputDialog(informativeMessage);	
 }

stultsuke and javaAddict, I tried both of your suggestions. The program is almost working. The sentinel value closes the window and prints the output as it's supposed to. Unfortunately, now it adds any input to the final value, not just valid input (in this case, integers from 0-110). I wanted to maybe have the program check if Q is pressed before parsing the string into double, but I do not know of a method that automatically closes a JOptionPane when it's showing an informativeMessage.

After debugging the program a few times, it seems that when a correct value is evaluated first, then an incorrect one (or the sentinel value), the result is always true. However, when an incorrect value (either a value less than zero or a value greater than 110) is evaluated first. then a correct value, the result is always false. I'm not sure why this is happening, because the method I wrote to check the entered values to see if they're invalid or not worked fine yesterday.

^ I did some further debugging. It seems that the previous entered value does not clear when the method I wrote to check if the value is valid. So, if I enter 90, this method will correctly evaluate it as a valid value. However, when I type -10, it still thinks the value is 90, so it always evaluates as true.

^Note: I fixed my earlier issue with the value not changing. Now, I'm just looking at another bug in my code.

I've finally got the program working. Thank you for your help. :)

commented: Thank you for marking your threlad as soved, so many people never do. +8
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.