0

When I enter a value that is in between than -2147483648 and 2147483647, the program closes and does not display the valid number like I wrote. If I enter a number outside the range, it is supposed to enter a while loop until I enter a valid number between the range. However when I enter a number outside the range it simply displays an exception error, which is why I put the catch there in the 1st place.

I have tried this problem for a few hours now, I am still relatively new to coding (2nd class) so I am sorry if this has been answered before. I looked up a lot of the older answers and tried to use that as a model for my code. However this is as far as I got.

import javax.swing.JOptionPane;

public class test
{
    public static void main (String[] args) throws NumberFormatException
    {
        String input;
        boolean x;
        int number;

        while (x = false)
        {
            try
            {
                input = JOptionPane.showInputDialog("Enter an integer: ");      //creates input box for user to enter integer value
                number = Integer.parseInt(input);

                if ( number < -2147483648 && number > 2147483647)

                    x = false;

                else
                {
                    x = true;
                    System.out.println("You entered: " + number);
                }

                break;
            }

            catch (NumberFormatException e)
            {
                continue;
            }
       }

   }

}
Matthew Osman
  • 85
  • 1
  • 2
  • 8

3 Answers3

1

Your if condition can never evaluate to true. Along with changing the while to

while (x == false)

as others have suggested, you also need to change the if statement from an AND to an OR:

if ( number < -2147483648 || number > 2147483647)

Also, you don't need a break statement after the else block. Since you are setting x to true, you will break out of the loop anyway. The way you have it now, you are breaking out of the loop on the first iteration.

Additionally, you should be initializing boolean x to false. At the top, you should have:

boolean x = false; where you have boolean x;

So when all that is said and done:

import javax.swing.JOptionPane;

public class test
{
    public static void main (String[] args) throws NumberFormatException
    {
        String input;
        boolean x = false;
        int number;

        while (x == false)
        {
            try
            {
                input = JOptionPane.showInputDialog("Enter an integer: ");      //creates input box for user to enter integer value
                number = Integer.parseInt(input);

                if ( number < -2147483648 || number > 2147483647)
                {
                    x = false;
                }
                else
                {
                    x = true;
                    System.out.println("You entered: " + number);
                }
            }
            catch (NumberFormatException e)
            {
                continue;
            }
       }
   }
}

And further cleaning it up. The parseInt throws an exception when the number is outside the desired range, and we're going into the catch block. So all you really need is:

import javax.swing.JOptionPane;

public class test
{
    public static void main (String[] args) throws NumberFormatException
    {
        String input;
        int number;

        while (true)
        {
            try
            {
                input = JOptionPane.showInputDialog("Enter an integer: ");      //creates input box for user to enter integer value
                number = Integer.parseInt(input);

                System.out.println("You entered: " + number);
                break;
            }
            catch (NumberFormatException e)
            {
                // do nothing
            }
       }
   }
}
dursk
  • 4,435
  • 2
  • 19
  • 30
0

First I did this

 while (x = false)    =>
 while (x == false)

Then Eclipse reminds that the boolean x is not initialized. As some of others pointed out, You can use other way like while(!x) but this the reason it fails.

Then, if ( number < -2147483648 && number > 2147483647) will always be false because it's int and the comparison is inverted.

Here is the full code after fix:

import javax.swing.JOptionPane;

public class MyTextPanel
{
    public static void main (String[] args) throws NumberFormatException
    {
        String input;
        long number;

        while (true) {
            try {
                input = JOptionPane.showInputDialog("Enter an integer: ");      //creates input box for user to enter integer value
                number = Long.parseLong(input);

                if ( number < -2147483648 || number > 2147483647)
                    continue;

                else  {
                    System.out.println("You entered: " + number);
                    break;
                }
            } catch (NumberFormatException e)  {
                continue;
            }
       }
   }
}
Splash
  • 1,288
  • 2
  • 18
  • 36
  • This was one issue, however it did not help with the compilation of my code in the way I wanted. The println still is not visible, and numbers outside the loop still do not enter the loop as they should – Matthew Osman Oct 07 '14 at 02:17
  • @MatthewOsman, what is the range that you want to print and exit? – Splash Oct 07 '14 at 02:39
0

How can a number be less than a negative number and greater than a positive number at the same time?

if ( number < -2147483648 && number > 2147483647)

change it to ||

Also I suggest that rather than using magic numbers, use Integer.MIN_VALUE and Integer.MAX_VALUE

Also as you are catching the NumberFormatException you should not also declare this as being thrown from main

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • i<10 && i>1 is a valid condition !!! im not saying his condition is valid but this should ans 'How can a number be less that something and greater than something at the same time?' – StackFlowed Oct 07 '14 at 02:25