0
import javax.swing.*;
import java.awt.*;

public class JCD {

    public static void main(String[] args) {

        String input, inputs;
        int input1, input2;

        input = JOptionPane.showInputDialog("Enter first number");
        inputs = JOptionPane.showInputDialog("Enter second number");

        input1 = Integer.parseInt(input);
        input2 = Integer.parseInt(inputs);

        JOptionPane.showMessageDialog(null, "The GCD of two numbers  " + input
                + "and" + inputs + " is: " + findGCD(input1, input2));

    }// close void

    private static int findGCD(int number1, int number2) {
        // base case
        if (number2 == 0) {
            return number1;

        }// end if

        return findGCD(number2, number1 % number2);
    }// end static

} // close class

What can I add so that it will only accept integers? If not given an integer then it will go back to ask again.....

MarvintOy
  • 9
  • 1
  • 3
  • 1
    This has been answered here as well http://stackoverflow.com/questions/3544521/user-input-validation-for-joptionpane-showinputdialog – tobad357 Jul 05 '15 at 15:31
  • 1
    One can use the approach of [DocumentFilter](https://docs.oracle.com/javase/tutorial/uiswing/components/generaltext.html), as stated in [this post](http://stackoverflow.com/a/9478124/1057230) and one can customize the `JOptionPane`, as shown in [this example](http://stackoverflow.com/a/12235299/1057230) DocumentFilter, will actually restrict user from entering any value other than an integer and one can customize the `JOptionPane` as per need. – nIcE cOw Jul 05 '15 at 15:44

3 Answers3

0

Put your input request in a while statement, check if it's an int, if not repeat the loop, otherwise exit. Do it for both your inputs.

Something like this

public static void main(String[] args) {

    String input=null, inputs=null;
    int input1 = 0, input2=0;

    boolean err=true;
    do{
        try{
            input = JOptionPane.showInputDialog("Enter first number");
            input1 = Integer.parseInt(input);
            err=false;
        }catch(NumberFormatException e){
            e.printStackTrace();
        }
    }while(err);

    err=true;
    do{
        try{
            inputs = JOptionPane.showInputDialog("Enter second number");
            input2 = Integer.parseInt(inputs);
            err=false;
        }catch(NumberFormatException e){
            e.printStackTrace();
        }
    }while(err);

    JOptionPane.showMessageDialog(null, "The GCD of two numbers  " + input
            + "and" + inputs + " is: " + findGCD(input1, input2));

}

Note that this solution requires you to initialize your variables when you declare them

    String input = null, inputs = null;
    int input1=0, input2=0;
Luigi Cortese
  • 10,841
  • 6
  • 37
  • 48
0

You should try with "try and catch". input = JOptionPane.showInputDialog("Enter first number");
inputs=JOptionPane.showInputDialog("Enter second number");

           try {

                     input1=Integer.parseInt(input);
                     input2=Integer.parseInt(inputs);
                    // establish and use the variables if the characters inserted are numbers
               }
         catch(NumberFormatException e){
                JOptionPane.showMessageDialog(null, e+ "is not a number");
                //display a warning to le the user know
               }
  • 1
    my apologies sir but where could i insert this? sorry for being annoying its just that looping and "try" "catch" is not discussed further by our "Good Instructor" – MarvintOy Jul 05 '15 at 16:03
  • I wont type the code, since is the waste of time. The idea behind this is that : you try to parse the input text into an int. If it is working, it means the user has inserted numbers. If is not working, it will give him a prompt, and tell him the character inserted is not a number. – Daniel Mogos Jul 05 '15 at 16:49
  • If you didnt learned all java, then why are you trying to make applications ?? Is not easier if you learn and then start coding? – Daniel Mogos Jul 05 '15 at 16:52
0

You could use something like this :-

boolean inputAccepted = false;
while(!inputAccepted) {
  try {
     input1=Integer.parseInt(input);
     input2=Integer.parseInt(inputs);
     inputAccepted = true;

  } catch(NumberFormatException e) {
    JOptionPane.showMessageDialog("Please input a number only");
  }

  ... do stuff with good input value
}
AnkeyNigam
  • 2,810
  • 4
  • 15
  • 23
  • yeah but where can i insert it? . . our good instructor did not discuss further "try" and "catch" also looping – MarvintOy Jul 05 '15 at 16:06