0
/* The following programs are written to guard the input. The input is requested as an  non-negative integer.Is there other method to do so? */

/* Operation on DOS window: "PositiveInt1" receives a positive integer only.  */
import java.util.*;
public class PositiveInt1{

    static int factorial(int n){
        int fac=1;
        if ((n<0) || (n>12))
        return -1;
        for (int i=2; i<=n;i++)
        fac *=i;
        return fac;
    }

    public static void main(String args[]){
        int n=0;
        Scanner in = new Scanner(System.in);

    while(true){
    try{
System.out.println("Input a non-negative integer:");
    n = in.nextInt();
    }catch(InputMismatchException e){
    System.out.println("Your input is mismatched.");
    in.next();
    continue;
    }
    if (n<0){
    System.out.println("You negative value is not valid.");
    continue;
    }
        break;
    }

    System.out.printf("Your input is %d\n", factorial(n));
    }
}
---------------------
/* via JOpationPane */
import javax.swing.*;
public class PositiveInt{
    static class NonNegativeException extends Exception{
    public NonNegativeException(String msg){
        super(msg);
    }   
    }
    static void show(int x)throws NonNegativeException {
        if (x<0)
        throw new NonNegativeException("Negative integer is not valid.");
    }
    public static void main(String args[]){
        int n=0;
    while(true){
        try{
        String s=JOptionPane.showInputDialog(null,"Input a positive integer:");
        n = Integer.parseInt(s);
        show(n);
        }catch(NumberFormatException e){
        System.out.println();
        JOptionPane.showMessageDialog( null, "Your input is mismatched. Try again.", "Warning ",JOptionPane.WARNING_MESSAGE);
        continue;
        }catch(NonNegativeException e){
        JOptionPane.showMessageDialog( null, e.getMessage(), "Warning ",JOptionPane.WARNING_MESSAGE);
        continue;   
        }
         break;    
    }
    JOptionPane.showMessageDialog(null,"Your input: "+n,"Welcome to DANIWEB", JOptionPane.INFORMATION_MESSAGE);
    }
}

Edited by Reverend Jim: Fixed formatting

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by NormR1
0

Is there other method

Yes, probably hundreds. You method is a bit more than is needed.
It could depend on your app's requirements

String[] input = {"a", "-1", "3"};

      for (int i=0; i < input.length; i++) {
         try{
           int val = Integer.parseInt(input[i]);
           if (val < 1)
             System.out.println("not positive integer: " + input[i]);
           else
             System.out.println("postive integer: " + input[i]);
         }catch(Exception x) {
           System.out.println("invalid integer:" + input[i]);
         }
      } // end for(i) thru input strings
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.