Results 1 to 4 of 4

Thread: Factorials help

  1. #1
    jhering1986 is offline Member
    Join Date
    Mar 2009
    Posts
    8
    Rep Power
    0

    Default Factorials help

    Ok so I have a Factorials file and I need to post an IllegalArgumentException error when the user types in a negative integer or an integer greater than 17 and I have it figured out, but sometimes the program doesn't function right. Sometimes when a negative integer is entered, it says 1 instead of IllegalArgumentException, but sometimes it works right. The same happens when something greater than 17 is entered. Here is my code:
    Java Code:
    package factorials;
    // ****************************************************************
    // Factorials.java
    //
    // Reads integers from the user and prints the factorial of each.
    //
    // ****************************************************************
    import java.util.Scanner;
    public class Factorials
    {
    public static void main(String[] args) throws IllegalArgumentException
    {
    Scanner sc = new Scanner(System.in);
            char keepGoing = 'y';
    
            while (keepGoing == 'y' || keepGoing == 'Y')
            {
                              try {
                            System.out.print("Enter an integer: ");
                            int val = sc.nextInt();
    
                            if (val < 0) { throw new Exception("IllegalArgumentException"); }
                            sc.nextLine(); // consume extra newline
                            System.out.println("Factorial(" + val + ") = "
                                    + MathUtils.factorial(val));
                            System.out.print("Another factorial? (y/n) ");
                            keepGoing = sc.nextLine().charAt(0);
                                            }
                    catch (Exception rtException)
                    {
                    System.err.println("Error: " + rtException.getMessage().toString());
                    }
                              try {
                            System.out.print("Enter an integer: ");
                            int val = sc.nextInt();
    
                            if (val > 16) { throw new Exception("IllegalArgumentException"); }
                            sc.nextLine(); // consume extra newline
                            System.out.println("Factorial(" + val + ") = "
                                    + MathUtils.factorial(val));
                            System.out.print("Another factorial? (y/n) ");
                            keepGoing = sc.nextLine().charAt(0);
                                            }
                    catch (Exception rtException)
                    {
                    System.err.println("Error: " + rtException.getMessage().toString());
                    }
    
                            }
        }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You're program is only doing what you tell it to. Half the time when you enter a number it checks if it's > 16 and the other half the time it checks if it is < 0. You need to rewrite it so that it checks both all the time (hint: don't have two attempts to get the number within your while loop, have only one attempt).

    In other words, you have this (pseudocode):
    Java Code:
    while (...)
    {
      get int
      check if it's less than 0
    
      get int
      check if it's greater than 16
    }
    and you want this:
    Java Code:
    while (...)
    {
      get int
      check if it's less than 0 || greater than 16
    }
    Last edited by Fubarable; 04-06-2009 at 12:07 AM.

  3. #3
    jhering1986 is offline Member
    Join Date
    Mar 2009
    Posts
    8
    Rep Power
    0

    Default

    Thank you, I figured that was what was going on.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •