I don't know why I'm spacing out tonight, because I've been doing fine, but I'm stuck on one thing. I've already written the first part of my program (get integer input from the user, then calculate the product, and display), but the second part (not required, but I thought I'd give it a try to find out how it works) says, "If the user enters a number (n) <= 0, you should inform then that this is invalid, and give them another chance to re-enter a number."

Thing is, I'm spacing out on how to go about this - to add it into my existing code. It looks easy, and I'm sure it is, but it's just not coming to me at the moment.

Here's my existing code:

import javax.swing.JOptionPane;


public class Summer
{

	public static void main(String args[])
	{

		int n;
		int product = 1;

		String input = JOptionPane.showInputDialog(null, "Please enter a number:",
		"Summer Program Input", JOptionPane.QUESTION_MESSAGE);


		n = Integer.parseInt(input);

		for (int i = 1; i <= n; i++)
		{
		product = product*i;
		}

		JOptionPane.showMessageDialog(null, "When the number is "+ n + ", the product = " + product,
		"Summer Program Product", JOptionPane.INFORMATION_MESSAGE);


	}

}

Any suggestions?

Recommended Answers

All 4 Replies

Hi newbieGirl
Here's the solution:

import javax.swing.JOptionPane;


public class Summer
{

    public static void main(String args[])
    {

        int n;
        int product = 1;
        String input;
        do {
input = JOptionPane.showInputDialog(null, "Please enter a number:",
        "Summer Program Input", JOptionPane.QUESTION_MESSAGE);
   }while("0".equals(input));


        n = Integer.parseInt(input);

        for (int i = 1; i <= n; i++)
        {
        product = product*i;
        }

        JOptionPane.showMessageDialog(null, "When the number is "+ n + ", the 

product = " + product,
        "Summer Program Product", JOptionPane.INFORMATION_MESSAGE);


    }

}

greetings
Parthiban

Thanks for the reply (and the help)!

I bascially did that same loop earlier tonight. :) It returns the user to the beginning of the loop. Though, I'm still stuck on the last part:

If the user inputs a number less than or equal to 0, an information message lets the user know they entered a number <= 0, and offers them another chance to enter a number (thus, restarting the loop).

Frustrating, because I love Java, but sometimes I just hit a wall, even if something seems so simple. lol

"If the user enters a number (n) <= 0, you should inform then that this is invalid, and give them another chance to re-enter a number."

Oh !!! ok .I didn't read carefully what you expected .

Here is what you expect:

do {
             input = JOptionPane.showInputDialog(null, "Please enter a number:","Summer Program Input", JOptionPane.QUESTION_MESSAGE);
                n =Integer.parseInt(input);
                if(n<=0)
                JOptionPane.showMessageDialog(null,"You entered an Invalid number");      
            }while(n<=0);


        for (int i = 1; i <= n; i++)
        {
        product = product*i;
        }

greetings
Parthiban

Again, thank you so much! I was putting if's and while's here and there - actually almost exactly as you had them - I was so close - but had one thing in the wrong spot. lol It all makes sense now...

Anyway, thanks again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.