0

I am trying to get my code to prompt an Error Message whenever the user enters a decimal number/negative number and will continue to loop until a positive number greater than 0 is implemented.

This is what I have so far;

public static void numberFunctions()
{   



    System.out.println("Calculating a Factorial");
    Scanner myScan= new Scanner (System.in);
    System.out.println("Please enter the number you'd like to use: ");
    int number = myScan.nextInt();

    System.out.println(number);       

        if(number>0)
    {
        int factorial = 1;
        int count;

        for (count = number; count >=1; count --)
        {
            factorial = factorial * count;

            System.out.print(count +" * ");


        }

        System.out.println(" = " + factorial);

    }
    else
    {

        System.out.println("ERROR! Please enter a positive number");

    } 
}
Somil Garg
  • 474
  • 4
  • 12
  • start here https://stackoverflow.com/questions/58926596/java-is-there-a-method-similar-to-hasnextint-or-hasnextdouble-for-strings – bananas Nov 19 '19 at 04:43

3 Answers3

0

First you can create a method for example

public static int getInteger() {

}

In that method you can ask the user to enter an integer using a try catch block inside a while loop so that way if they enter a double or a String it will catch the Exception and ask the user to try to enter an integer again.

Once you figure that out you can call that method and check if the number returned is greater than 0.

Once you create this method you can always use it to ask for integers from the user.

greenapples
  • 64
  • 1
  • 9
0

Add a try/catch block while taking input.

try {
    int number = myScan.nextInt();
}
catch(InputMismatchException e) {
    System.out.println("Enter integer numbers only");
    System.out.println(scanner.next() + " was not valid input.");
}
Somil Garg
  • 474
  • 4
  • 12
  • `OP` asked if user enters `decimal number/negative`. How is your solution going to help? – bananas Nov 19 '19 at 04:40
  • adding a try catch will not stop the execution of the code and it will go to the next iteration. Where it will ask for a integer number. – Somil Garg Nov 19 '19 at 04:41
  • check here: https://stackoverflow.com/questions/58926596/java-is-there-a-method-similar-to-hasnextint-or-hasnextdouble-for-strings – bananas Nov 19 '19 at 04:43
0

You are doing ok with the negative number checking part. However if the user enter an invalid string or a real number, this line of code will throw exception so we need to catch that error: int number = myScan.nextInt(); It is also a good practice to always close your Scanner after using it so we will put it in the finally part. Here is my suggested code for you:

public static void  numberFunctions()
    {   
        System.out.println("Calculating a Factorial");
        Scanner myScan= new Scanner (System.in);
        System.out.println("Please enter the number you'd like to use: ");
        try {
            int number = myScan.nextInt();
            if(number>0)
            {
                int factorial = 1;
                int count;
                for (count = number; count >=1; count --)
                {
                    factorial = factorial * count;
                    System.out.print(count);
                    if (count != 1) {
                        System.out.print(" * ");
                    }
                }
                System.out.println(" = " + factorial);
            }else {
                System.out.println("ERROR! Please enter a positive number");
            }
        }catch(InputMismatchException ex) {
            System.out.println("ERROR! Please enter a positive number");
        }finally {
            myScan.close();
        }
    }