0

I wrote this programme where the user enters the nth term of the fibonachi sequence. So, if I typed 2, a message would appear that the second term of the Fib sequence is 1. I also want to output a message of the arrays upto the term they typed. E.g. is the user typed in 10, a message would appear showing all the numbers up to the 10th term in the fib sequence. Problem is that I dont know how to do that. It gives me a wierd number like @2235f23 (something like that).

My code:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.security.cert.X509Certificate;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class maincard extends JFrame{   

    JLabel label;
    JTextField txf;
    JButton calculate;

    public static void main(String[] args){

        new maincard();


    }

    public maincard(){

        thehandler handler = new thehandler();

        JPanel panel = new JPanel();

        this.setSize(460, 360);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.setTitle("Fibonachi Seqeuence");
        this.add(panel);

        label = new JLabel("Enter the nth term to find the value of the corresponding Fibonachi Sequence");
        label.setToolTipText("What are you waiting for? Enter a damn number!!");
        label.setVisible(true);
        panel.add(label);

        txf = new JTextField("Term");
        txf.setSize(40, 25);
        txf.addActionListener(handler);
        txf.setVisible(true);
        panel.add(txf);

        calculate = new JButton("Calculate");
        calculate.addActionListener(handler);
        calculate.setSize(40, 60);      
        calculate.setVisible(true);
        panel.add(calculate);





    }

    private class thehandler implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            if(e.getSource() == calculate){
                int fibTerm = Integer.parseInt(txf.getText());
                int answer;

                int[] x = new int[fibTerm];

                x[0] = 0;
                x[1] = 1;

                for(int y = 2; y<x.length; y++){
                    x[y] = x[y-1] + x[y-2];

                }

                answer = x[fibTerm-1];
                JOptionPane.showMessageDialog(null, "Sequence up to the " + fibTerm + "is: " + x);
                JOptionPane.showMessageDialog(null, "The term is: " + answer);
            }

        }
    }

}

Oh and another question, finding the nth term works pefect but I dont see why it works when I write 'answer = x[fibTerm-1]' but doesn't work when I write 'answer=x[fibTerm]'

greg-449
  • 109,219
  • 232
  • 102
  • 145
Tloz
  • 307
  • 1
  • 3
  • 12

2 Answers2

0

Print it as list (after conversion) using Arrays.asList

igr
  • 3,409
  • 1
  • 20
  • 25
0

Modify your actionPerformed(ActionEvent e) method:

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == calculate) {
        int fibTerm = Integer.parseInt(txf.getText());
        int answer;
        ArrayList<Integer> answers = new ArrayList<Integer>();
        int[] x = new int[fibTerm];
        x[0] = 0;
        x[1] = 1;
        for (int y = 2; y < x.length; y++) {
            x[y] = x[y - 1] + x[y - 2];
            answers.add(x[y]);
        }
        answer = x[fibTerm - 1];
        JOptionPane.showMessageDialog(null, "Sequence up to the "
                + fibTerm + " is: " + answers.toString());
        JOptionPane.showMessageDialog(null, "The term is: " + answer);
    }
}
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73