0

Just beginning to learn java so this has had me stuck for quite some time now. This is a program to output numbers of the Fibonacci sequence. The goal here is to get the program to output only the last line of the while loop. I've got the program up and running, just having trouble trying to figure out how to only output the last line of the sequence

Example Input/Output Input: 5

Output: Fibonacci #3 is: 3 Fibonacci #4 is: 5 Fibonacci #5 is: 8

Output should look like this: Fibonacci #5 is: 8

import javax.swing.JOptionPane;

public class Fibonacci2 {

    public static void main(String args[ ]) {

       int n, 
           counter = 2, 
           sum,    
           prevN, 
           nextN; 

       String inputValue;

       inputValue = JOptionPane.showInputDialog(null, "Enter Number Greater than 2: ",
                              JOptionPane.QUESTION_MESSAGE);
       n = Integer.parseInt(inputValue);

       if (n >= 2)

       counter = 2; 
       prevN = 1;
       nextN = 2;

       if (n < 2) 
       System.out.println("Invalid Input. Please Try Again.");


       while (counter < n)
       {
           sum = prevN + nextN;
           prevN = nextN;
           nextN = sum; 
           counter++;

           System.out.println("Fibonacci #"+counter+" is: " + sum); 

       }

       System.exit(0);

    } 

} 
Stephen P
  • 14,422
  • 2
  • 43
  • 67
  • 2
    Have you tried just moving the `System.out.println("Fibonacci #"+counter+" is: " + sum); ` to just above the `System.exit(0);` statement (outside of the loop)? I'm also concerned about your `if` statements, not using `{ }` to contain the context means only the next line will be execute when `true`, which doesn't seem to be your intent – MadProgrammer Oct 02 '18 at 22:32
  • I've tried that, it gives me this: Fibonacci2.java:62: error: variable sum might not have been initialized System.out.println("Fibonacci #"+counter+" is: " + sum); ^ – Jake Schlott Oct 02 '18 at 22:37
  • 1
    The change `sum,` to `sum = 0,` when you create the variable references – MadProgrammer Oct 02 '18 at 22:38
  • It worked! Thank you so much for your help! – Jake Schlott Oct 02 '18 at 22:41

4 Answers4

2

The simple answer to your question is, move the System.out.println("Fibonacci #" + counter + " is: " + sum); statement outside of the while-loop context...

while (counter < n) {
    sum = prevN + nextN;
    prevN = nextN;
    nextN = sum;
    counter++;
}

System.out.println("Fibonacci #" + counter + " is: " + sum);

I've tried that, it gives me this: Fibonacci2.java:62: error: variable sum might not have been initialized System.out.println("Fibonacci #"+counter+" is: " + sum); ^

Unlike instance fields, local variables are not assigned a default value when you create them, so you need to ensure that the value has a default value if the path of the code can not guarantee it will be set...

int n,
        counter = 2,
        sum = 0,
        prevN,
        nextN;

Additionally, your if statements are of concern...

if (n >= 2)

counter = 2; 
prevN = 1;
nextN = 2;

if (n < 2) 
System.out.println("Invalid Input. Please Try Again.");

Basically what this is saying is...

  • if n is greater or equal to 2, then counter equals 2
  • prevN equals 1
  • nextN equals 2
  • if n is less then 2 then print error message
  • Continue to run loop

To me, this doesn't make a lot of sense and is difficult to read. Instead, you should contain the code which should be executed for each branch within it's own execution context (ie, between {...} braces), for example...

if (n >= 2) {
    counter = 2;
    prevN = 1;
    nextN = 2;

    while (counter < n) {
        sum = prevN + nextN;
        prevN = nextN;
        nextN = sum;
        counter++;
    }

    System.out.println("Fibonacci #" + counter + " is: " + sum);
} else {
    System.out.println("Invalid Input. Please Try Again.");
}

Which makes the code easier to read and to understand - but I'm simple minded

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

Just put it outside loop

while (counter < n){
sum = prevN + nextN;
prevN = nextN;
nextN = sum; 
counter++; 
}
System.out.println("Fibonacci #"+counter+" is: " + sum);
}
bakero98
  • 805
  • 7
  • 18
  • I've tried this, only to give me this error: Fibonacci2.java:62: error: variable sum might not have been initialized System.out.println("Fibonacci #"+counter+" is: " + sum); ^ – Jake Schlott Oct 02 '18 at 22:34
  • 1
    @JakeSchlott At the start you have to give value to sum, because if n = 0 while loop is not going to happen so sum is not initialized. At the start make `int sum = 0;` – bakero98 Oct 02 '18 at 23:05
1

The only thing you have to do is to take outside of the loop the System.out.println("Fibonacci #"+counter+" is: " + sum) that way it only prints the last number; Like this:

while (counter < n)
   {
   sum = prevN + nextN;
   prevN = nextN;
   nextN = sum; 
   counter++;
   }
System.out.println("Fibonacci #"+counter+" is: " + sum);
Bcct
  • 27
  • 5
0

I made a similar program for my EGR class; its goal is to output a number given a position (sequence begins at 1) i.e. p1=1, p2=1, p3=2, p4=3, p5=5, p6=8, etc.

import java.util.Scanner;

public static void main(String[] args) {
Scanner keyboard=new Scanner(System.in);

System.out.println("Enter the Fibonacci Number's position.");
long n=keyboard.nextLong();
long sum, prevN=1, nextN=0, counter=1, last=0, lastnum;


    while (counter <= n) {
        sum = prevN + nextN;
        prevN = nextN;
        nextN = sum;
        if (n == counter)
        {
             System.out.println("Fibonacci #" + counter + " is: " + sum);
        }
        counter++;      
    }
    
    

}}

I know that it's way late for OP but that's my two cents for any other students/enthusiasts looking for assistance.