-3

How am I supposed to print this to my console and make it show an array that i declared in my method?

import java.util.Arrays;
public class FibonacciSequence
{
    public int fibonacci( int numArray[])
    {

        for ( int i = 0; i < numArray.length; i++ )
        {
            System.out.println( numArray[i] + numArray[i-1]);
        }

        return numArray[10];

    }


    public static void main(String[] args)
    {


    }
}

When im trying to print it like:

System.out.println(fibonacci());

it doesn't work, so how am I supposed to do this?

I just want to know how to print it in my console.

dreamcrash
  • 47,137
  • 25
  • 94
  • 117
joedoe
  • 49
  • 1
  • 2
  • 9

3 Answers3

3

First your method won't print the good fibonnaci numbers, and to print it you need to instanciate a FibonacciSequence Object since the method is not static :

public int fibonacci(int numArray[]) {
    for (int i = 1; i < numArray.length - 1; i++) {
        System.out.print(numArray[i] + numArray[i - 1] + " ");
    }
    return numArray[10];
}

public static void main(String[] args) {
    FibonacciSequence fbs = new FibonacciSequence();
    fbs.fibonacci(new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10});      
}

Prints : 1 3 5 7 9 11 13 15 17


If you don't want to use recursion, you can do like this (and see how to print easily an array):

public int[] fibonacci(int size) {
    int[] res = new int[size];
    res[0] = 0;
    res[1] = 1;
    for (int i = 2; i < size; i++) {
        res[i] = res[i - 1] + res[i - 2];
    }
    return res;
}

public static void main(String[] args) {
    FibonacciSequence fbs = new FibonacciSequence();
    int[] arrayREs = fbs.fibonacci(10);
    System.out.println(Arrays.toString(arrayREs));
}

Prints : [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

azro
  • 53,056
  • 7
  • 34
  • 70
2

You can print everything by: System.out.println(/* content to print */)

You need to call it in you main

It‘s easy for primitive datatypes like int, double, etc but a little more complex if you want to print something like an array.

Start First with writing the Code Directly in your Main to understand how the print methods work for different types and then Extract it into a method.

Hint: you can store your Return value (i.e. the Array) First into a temporary variable and then print it out to console. Methods that dont need a object To be created need to be Static to be called out of the Main method. You also can look for the .toString() method of the array class. Because Strings are easier to print (and to read in Console)

Serelias
  • 98
  • 9
2

If I understand correctly, you would like to do fibonacci of passed values as well as printing original array. So something you would try like:

public class FibonacciSequence {

    /**
     * @param args
     */
    public int[] fibonacci(int numArray[]) {

        for (int i = 1; i < numArray.length; i++) {
            System.out.println(numArray[i] + numArray[i - 1]);
        }

        return numArray;


    }

    public static void main(String[] args) {

        int a[]=new int []{1,2,3,4,5};

        System.out.println(""+Arrays.toString(new FibonacciSequence().fibonacci(a)));


    }

}

Moreover, there are few good replies and answers at StackOverflow, you can find one at SO - What's the simplest way to print a Java array?.

Zabuzard
  • 25,064
  • 8
  • 58
  • 82
Ashish Patil
  • 4,428
  • 1
  • 15
  • 36