This discussion is archived
3 Replies Latest reply: Sep 20, 2013 9:29 AM by PhHein RSS

Fibonacci series using Thread

d8cf9d2b-5506-493a-b991-ac0ed504768a Newbie
Currently Being Moderated

I am trying to write a program that generate Fibonacci series.

Sample Input: 15

Output: 0 1 1 2 3 5 8 13

 

Whenever I run the program it doesn't show the result properly. Print only 0 ,1

 

import java.util.Scanner;

import java.awt.event.*;

 

class ChildProcess implements Runnable{

    public void run(){     

         while(true) {                 

            Scanner scan= new Scanner(System.in);

            System.out.print("$ fib : ");                         

            if(scan.hasNextInt() ){               

                if(scan.nextInt() <=0) {

                    System.out.println("Please input an integer number greater than 0 (Zero)");

                    System.out.println("************   Type 'Exit' to Quit.  *************");           

                }

                else{                  

                    int f1=0, f2=1, f3;        

                    System.out.print(f1+" "+ f2);

                    f3=f1+f2;

                    while(f3<scan.nextInt()){

                        System.out.print(" "+f3);

                        f1=f2;

                        f2=f3;

                        f3=f1+f2;

                    }                                                                                   

                }               

            }             

            else if("exit".equalsIgnoreCase(scan.next())){

                System.exit(1);

            }                                           

           else {

                System.out.println("Please input an integer number greater than 0 (Zero)");

                System.out.println("************   Type 'Exit' to Quit.   ************");                           

            }                                                             

        }

    }

}

 

class MainClass {

    public static void main(String args[]){       

        ChildProcess cp = new ChildProcess();

        Thread cpThread = new Thread(cp);

        cpThread.start();        

    }

}

 

************************************************

The red lines are not executed.

But, if i run the red lines in a simple main() function, it works fine.

I can't find the problem. Please help me ou.

 

Thanks.

  • 1. Re: Fibonacci series using Thread
    user8729783 Newbie
    Currently Being Moderated

    Hi

    Below is the Code snippet working fine.

     

    class ChildProcess implements Runnable{

     

        public void run(){    

     

             while(true) {

                

     

                 Scanner scan= new Scanner(System.in);

                 int input=0;

                

                 System.out.print("$ fib : ");                        

                 if(scan.hasNext())

                     input=scan.nextInt();

                           

     

                     if(input <=0) {

     

                         System.out.println("Please input an integer number greater than 0 (Zero)");

     

                         System.out.println("************   Type 'Exit' to Quit.  *************");          

     

                     }

     

                     else{                 

     

                         int f1=0, f2=1, f3;       

     

                         System.out.print(f1);

     

                         f3=f1+f2;

     

                        for(int i=0;f3<input;i++){

                            System.out.print(" "+f3);

                            f1=f2;

                            f2=f3;

                            f3=f1+f2;

                           

                        }

     

                     }              

     

                      System.exit(0);       

     

               

             

           

             }

     

        }

     

    }

     

     

    ------------------------------------------------------

    dont use scan.nextInt() method it will look for another integer.

     

    Thanks

  • 3. Re: Fibonacci series using Thread
    PhHein Guru Moderator
    Currently Being Moderated

    Normally I should have deleted user8729783 's reply for spoon feeding. No beginner will learn by copying ready made code.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points