1

Any idea on how I can fix this? I'm telling the user to enter the amount of numbers he would like to average. Of course as you can see in the code, if he inputs a 0 or a negative number I want it to flag the user to enter another number that is not 0 or a negative number. The problem is, once the user puts a 0 or a negative number, it gets stuck in that state and I have to terminate the program.

Help?

import javax.swing.JOptionPane;

public class TestProgTres 
{
    public static void main(String[] args) 
    {
         //Variable Declaration
         String ShowSome;
         String ShowSomeAgain;
         int z           = 0;
         double avg      = 0;
         double totalamt = 0;

         ShowSome = JOptionPane.showInputDialog("Enter the amount of numbers you would like to average"); 
         double AllNumbers = Double.parseDouble(ShowSome);

         while (AllNumbers < 1)
         {
             JOptionPane.showInputDialog("You cannot enter a negative or a 0.  Enter the amount of numbers you would like to average");
             AllNumbers = Double.parseDouble(ShowSome);
         }//end while

             if (AllNumbers > 0)
             {
                 double Numbers [] = new double [(int) AllNumbers];

                 for (z = 0; z < Numbers.length; z++)
                 {
                     ShowSomeAgain= JOptionPane.showInputDialog("Enter number " + (z + 1));
                     Numbers[z]=Double.parseDouble(ShowSomeAgain);

                     totalamt += Numbers[z];
                     avg = totalamt/AllNumbers;
                 }
             }

              JOptionPane.showMessageDialog(null, "The  of the numberes entered is " + avg);
        }//end main
}// end class
1
  • You're not assigning to ShowSome and are ignoring the results from JOptionPane.showInputDialog Commented Jul 29, 2014 at 4:35

3 Answers 3

3

Well, it isn't the cleanest but this has a fairly important error,

ShowSome = JOptionPane.showInputDialog("Enter the amount of numbers "
    + "you would like to average"); 
double AllNumbers = Double.parseDouble(ShowSome);

while (AllNumbers < 1)
{
  JOptionPane.showInputDialog("You cannot enter a negative or a 0. "
      + "Enter the amount of numbers you would like to average");
  // HERE!
  AllNumbers = Double.parseDouble(ShowSome);
}//end while

You need to prompt for and update ShowSome.

while (AllNumbers < 1)
{
  // HERE!
  ShowSome = JOptionPane.showInputDialog("You cannot enter a negative or a 0. "
      + " Enter the amount of numbers you would like to average");
  // Get it again.
  AllNumbers = Double.parseDouble(ShowSome);
}//end while

Also, the Java naming convention would name your variables starting with a lower case letter. Using showSome would be easier to read ShowSome looks like a class name. Same with allNumbers.

Sign up to request clarification or add additional context in comments.

2 Comments

As an alternative, a do-while loop might be a better structure to use, cause you want to do at least one iteration of the loop...just saying ... and why are you sowing the input dialog twice?
Know that feeling, looks better now +1
2

Basically, you want to ask the user at least once to enter a value, so why not just reduce the entire work load down to a single do-while loop, for example...

double AllNumbers = 0;
do {

    String ShowSome = JOptionPane.showInputDialog("Enter the amount of numbers you would like to average"); 
    AllNumbers = Double.parseDouble(ShowSome);
} while (AllNumbers < 1);

The main problem you're having with your current loop is you are ignoring the return value from the JOptionPane and continuously parsing what was previously entered...

// ShowSome is now set to, let's say 0...
ShowSome = JOptionPane.showInputDialog("Enter the amount of numbers you would like to average"); 
// AllNumbers  is now 0
double AllNumbers = Double.parseDouble(ShowSome);

while (AllNumbers < 1)
{
     // Prompting for a value, but you are ignoring it...
     JOptionPane.showInputDialog("You cannot enter a negative or a 0.  Enter the amount of numbers you would like to average");
     // Parsing ShowSome which is still 0 (as an example)...
     AllNumbers = Double.parseDouble(ShowSome);
 }//end while

3 Comments

any idea on how to ensure that a user doesn't input a decimal or a letter? I want to make sure that what the user puts in is a whole number now that I am testing this program.
Quick question. I just did this: do { try { String ShowSome = JOptionPane.showInputDialog("Enter numbers."); AllNumbers = Integer.parseInt(ShowSome); } catch(NumberFormatException e) { JOptionPane.showMessageDialog(null, "Value must be an integer!"); } } How do I make it so when the user clicks cancel, the error message doesn't pop up?
From memory, if the user presses cancel or closes the dialog via the [x] button, JOptionPane will return null...
2

Move following code inside the loop to prompt for input every time when user enters wrong input and break loop while you get what you want.

     //Declare ShowSome and AllNumbers outside loop
     while (true){
          try{
            ShowSome = JOptionPane.showInputDialog("Enter....."); 
            AllNumbers = Double.parseDouble(ShowSome);
            //May Throw exception for invalid input So be careful with this 
            if(AllNumbers>=1)break;
            JOptionPane.showInputDialog("You cannot enter a negative or a 0"); 
          }catch(NumberFormatException e){
          JOptionPane.showInputDialog("Not a valid Number!"); 
          //Not Recommended to swallow the exception
          }
     }

Secondly there is no meaning of checking if (AllNumbers > 0) after doing this as above code will prompt until user enters wrong input so you will definitely get proper value which is >=1.

You should use try-catch mechanism to avoid Exception for invalid input.

Moreover you should declare AllNumbers as integer to avoid cast.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.