1

I want this program to take an array list "max" that the size is created by the user. Then the program uses the Fibonacci sequence to store all the Fibonacci numbers in the arraylist that are less than the arraylist "max" which is created by the user input.

import java.util.*;
import javax.swing.*;

 public class FibonacciArrayList {

 public static ArrayList<Integer> Fibonacci (Integer Max){

     ArrayList<Integer> A = new ArrayList<Integer>();
   int n0;
   int n1;
   int n2;

   for(int i= 0; i = max; i++){
     n2= n1 + n0;
     system.out.println(n2);
     n0=n1;
     n1=n2;

  return A;
}


  public static void main (String[] arg){
    Integer max;
    String Title = "Fibonacci ArryList";
    String data = JOptionPane.showInputDialog(null, "Enter the upper bound",    Title, 1);
  max = new Integer(data);
    ArrayList<Integer> A = Fibonacci(max);
    System.out.println("There are " + A.size()+ " Fibonacci numbers less than "+max);

    }

}
1
  • Set your n0 = 0, your n1 = 1...right now they don't even have a value. Also, it's System.out.println(n2), with a capital S. You need to add numbers to your array list with A.add(0). It seems like you are adding max integers to the array list, but instead it sounds like you wanted to only add integers that were less than max. Commented Sep 21, 2015 at 2:26

2 Answers 2

2

Modified and simplified the logic to the Fibonacci function. Comments have been added to help you understand the changes.

public static ArrayList<Integer> Fibonacci (Integer max) { //Instead of 'Max' use 'max'
    ArrayList<Integer> A = new ArrayList<Integer>();

    //Initialize value of n0, n1 and n2
    int n0=0;
    int n1=1;
    int n2=1;

    //Handling the base conditions
    if(max == 0) return A;

    if(max == 1) {
        A.add(n0);
        return A;
    }

    //Add first 2 elements in the array
    A.add(n0);
    A.add(n1);

    //A 'while' loop will be more suitable to what you want to achieve
    while(n2 < max) {
        A.add(n2); //Instead of printing the values, add them into ArrayList A
        n0=n1;
        n1=n2;
        n2 = n1 + n0;
    } //Add a closing bracket for the 'for' loop

    return A;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try this code.

import java.util.*;
import javax.swing.*;

 public class FibonacciArrayList {

 public static ArrayList<Integer> Fibonacci (int Max){

     ArrayList<Integer> A = new ArrayList<Integer>();
   int n0=0;
   int n1=1;
   int n2;
   if(Max==0){

   }
   else if(Max==1)
   {
       System.out.println(n0);
       A.add(n0);
   }
   else if(Max==2)
   {
       System.out.println(n0);
       System.out.println(n1);
       A.add(n0);
       A.add(n1);
   }

   else{
       System.out.println(n0);
       System.out.println(n1);
       A.add(n0);
       A.add(n1);
   for(int i= 2; i <=Max; i++){
     n2= n1 + n0;
     A.add(n2);
     System.out.println(n2);
     n0=n1;
     n1=n2;
   }
   }
  return A;
}


  public static void main (String[] arg){
    int max;
    String Title = "Fibonacci ArryList";
    String data = JOptionPane.showInputDialog(null, "Enter the upper bound",    Title, 1);
  max = Integer.parseInt(data);
    ArrayList<Integer> A = Fibonacci(max);
    System.out.println("There are " + A.size()+ " Fibonacci numbers less than "+max);

    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.