Here's my code:

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class DisplayGUI extends JFrame
{
	public static void main(String args[]) throws Exception
	{
	    ButtonFrame buttonFrame = new ButtonFrame();
	    buttonFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    buttonFrame.setSize(275, 75);
	    buttonFrame.setVisible(true);

	}

    private JButton Fibonacci;
    private JButton Factorial;

    public DisplayGUI()
    {
        setLayout(new FlowLayout());

    	Factorial= new JButton("Factorial");
    	add(Factorial);
    	Fibonacci = new JButton("Fibonacci");
        add(Fibonacci);

    	ButtonHandler handler = new ButtonHandler();
    	Factorial.addActionListener(handler);
    	Fibonacci.addActionListener(handler);
    }

    private class ButtonHandler implements ActionListener
    {
        int a=0,ctr=0,rep=1,x,b=0;
		String strNum = JOptionPane.showInputDialog(null, "Input integer number: ", "Input", JOptionPane.QUESTION_MESSAGE);
      	int Num=Integer.parseInt(strNum);

        public void actionPerformed(ActionEvent event)
        {
			Object source = event.getSource();
			if(source == Factorial)
			{
				a=Num;
				ctr=Num;
				while (ctr>1)
				{
				    ctr--;
				    a=a*ctr;
				}
                JOptionPane.showMessageDialog(null, "Factorial of "+Num+": "+a+"\n", "Factorial", JOptionPane.INFORMATION_MESSAGE);
			}
			if(source == Fibonacci)
			{
				ctr=1;
				int n=0;
				int fib=1;
				if (Num==0)
				{
					JOptionPane.showMessageDialog(null, "0", "Fibonacci", JOptionPane.INFORMATION_MESSAGE);
				}
				else
				{
				    while (fib<Num)
				    {
				        if (ctr==1)
				        {
							JOptionPane.showMessageDialog(null, ""+fib+"", "Fibonacci", JOptionPane.INFORMATION_MESSAGE);
							a=fib;
						}
				        else if (ctr==2)
				        {
							JOptionPane.showMessageDialog(null, "1", "Fibonacci", JOptionPane.INFORMATION_MESSAGE);
							b=1;
						}
						else
						{
							fib = (Num-1)+(Num-2);
							for(int ctr=0;ctr<=10; ctr++);
							JOptionPane.showMessageDialog(null, ""+ctr+"", "Fibonacci", JOptionPane.INFORMATION_MESSAGE);
						}
					}
				}
        	}
		}
	}
}

My problem here is that when I try the fibonacci button, it shows the output one by one.

ex. input 15

1 then OK

1 then OK

2 then OK

and so forth...

What I need is to output the whole series in one dialog box like this...

1 1 2... and so forth...

Can anyone tell me what to do

Recommended Answers

All 12 Replies

use array to store the result, and at last show the result.

How exactly can I do that?

I edited the last else statement a bit like this:

fib = (Num-1)+(Num-2);
							for(int ctr=0; ctr<=10; ctr++);
							for(i=0; i<Num; i++)
							{
								fiboCount[i]=ctr;

							}
							String output = String.format(""+fiboCount[i]+"");
							JOptionPane.showMessageDialog(null, output);

but it still has the same result

I tried to test out your code but was unable to because you have not posted the ButtonFrame class code:

public static void main(String args[]) throws Exception
	{
	    ButtonFrame buttonFrame = new ButtonFrame();
	    buttonFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    buttonFrame.setSize(275, 75);
	    buttonFrame.setVisible(true);

	}

The whole code is actually on my first post. The previous code I put is an edited version of only the last else statement in the original code in my first post.

The whole code is actually on my first post. The previous code I put is an edited version of only the last else statement in the original code in my first post.

I'm looking through this entire thread. I don't see ButtonFrame class code anywhere, including the first post. In particular, I don't see the ButtonFrame() constructor. Did I miss it? Is it on a prior thread?

You did miss it. It's actually the first part of my code

I put it here to save you the trouble

public class DisplayGUI extends JFrame
{
	public static void main(String args[]) throws Exception
	{
	    ButtonFrame buttonFrame = new ButtonFrame();
	    buttonFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    buttonFrame.setSize(275, 75);
	    buttonFrame.setVisible(true);

	}

    private JButton Fibonacci;
    private JButton Factorial;

    public DisplayGUI()
    {
        setLayout(new FlowLayout());

    	Factorial= new JButton("Factorial");
    	add(Factorial);
    	Fibonacci = new JButton("Fibonacci");
        add(Fibonacci);

    	ButtonHandler handler = new ButtonHandler();
    	Factorial.addActionListener(handler);
    	Fibonacci.addActionListener(handler);
    }

I changed the ButtonFrame() constructor to DisplayGUI

You did miss it. It's actually the first part of my code

I put it here to save you the trouble

public class DisplayGUI extends JFrame
{
	public static void main(String args[]) throws Exception
	{
	    ButtonFrame buttonFrame = new ButtonFrame();
	    buttonFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    buttonFrame.setSize(275, 75);
	    buttonFrame.setVisible(true);

	}

    private JButton Fibonacci;
    private JButton Factorial;

    public DisplayGUI()
    {
        setLayout(new FlowLayout());

    	Factorial= new JButton("Factorial");
    	add(Factorial);
    	Fibonacci = new JButton("Fibonacci");
        add(Fibonacci);

    	ButtonHandler handler = new ButtonHandler();
    	Factorial.addActionListener(handler);
    	Fibonacci.addActionListener(handler);
    }

I changed the ButtonFrame() constructor to DisplayGUI

So then this line would have to change from this:

ButtonFrame buttonFrame = new ButtonFrame();

to this:

DisplayGUI buttonFrame = new DisplayGUI();

since there is no ButtonFrame class. Correct?

Yes, forgot to change that part, my bad.

or add it all to a single String object.
String number = "";

and every time you have found your new number:

number += " " + numberYouHaveJustFound;

while (fib<Num)
				    {
				        if (ctr==1)
				        {
							JOptionPane.showMessageDialog(null, ""+fib+"", "Fibonacci", JOptionPane.INFORMATION_MESSAGE);
							a=fib;
						}
				        else if (ctr==2)
				        {
							JOptionPane.showMessageDialog(null, "1", "Fibonacci", JOptionPane.INFORMATION_MESSAGE);
							b=1;
						}
						else
						{
							fib = (Num-1)+(Num-2);
							for(int ctr=0;ctr<=10; ctr++);
							JOptionPane.showMessageDialog(null, ""+ctr+"", "Fibonacci", JOptionPane.INFORMATION_MESSAGE);
						}
					}

You're in an infinite loop here if ctr == 1 or ctr == 2. If either the "if" block or the "else if" block is executed, the ctr, fib, and Num variables will never change. Since they are the loop control variables, you have an infinite loop.

Thanks for the replies. I finally finished the code.

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.