Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: The sum and product of a positive number between 1000 and 9999.

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    10
    My Mood
    Amused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default The sum and product of a positive number between 1000 and 9999.

    I wrote a program that asks the user for a number and displays the original number, the sum of the digits and the product of the digits. I need to add an ERROR message if the number entered is not positive or between 1000 and 9999. I also need it to display "Zero" if the product of the four digits is 0. I am just trying to figure out the easiest way of accomplishing this task. This is what I have so far:


    import javax.swing.JOptionPane;

    public class Program2
    {
    public static void main(String[] args)
    {

    String userInput;
    int posFourDigits;
    String output, output1, output2;

    userInput = JOptionPane
    .showInputDialog("Enter a positive four digit number between 1000 and 9999: ");
    posFourDigits = Integer.parseInt(userInput);

    output = "The original number is: " + (posFourDigits) + "\n";
    output1 = "The sum of the four digits is: "
    + ((posFourDigits % 10) + (posFourDigits / 10 % 10)
    + (posFourDigits / 100 % 10) + (posFourDigits / 1000))
    + "\n";

    output2 = "The product of the four digits is: "
    + ((posFourDigits % 10) * (posFourDigits / 10 % 10)
    * (posFourDigits / 100 % 10) * (posFourDigits / 1000))
    + "\n";
    JOptionPane.showMessageDialog(null, output + output1 + output2,
    "Program 2 (Artemus Watson)",

    JOptionPane.PLAIN_MESSAGE);

    System.exit(0);
    }
    }


  2. #2
    Junior Member switcha's Avatar
    Join Date
    Sep 2011
    Location
    Australia
    Posts
    20
    My Mood
    Fine
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: The sum and product of a positive number between 1000 and 9999.

    Hi metaleddie13,
    Your issues sound like general exception - handling issues. Have you considered using the try...catch statement to determine if the number is not positive? You can also use this to determine if the number is between 1000 and 9999.
    Remember, try...catch statements should always go from most specific to most general. This is because if you catch the most general exception first, it will account for all of the exceptions, making it harder to understand what the specific error is. This will also enable you to display a 0.
    You should be able to code the user input request in the try block and the exceptions afterwards.
    Good luck and let me know if you solve it
    ~switch~

Similar Threads

  1. How to returned random number to original number?
    By i4ba1 in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 19th, 2011, 04:35 AM
  2. Number Array
    By TheJavaGuy in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 17th, 2011, 04:59 AM
  3. [ask] about sorting number.
    By bontet in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 30th, 2010, 02:07 PM
  4. Help With Odd/Even number program
    By JonoScho in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 23rd, 2009, 10:53 AM
  5. Reverse Number
    By java1 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 28th, 2009, 10:19 AM