Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Fibonacci series

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Fibonacci series

    Hey Im new to JAVA programming i have alot of experience in C/C++ but not java was just wondering want i am doing wrong in this code. I have to take a input from the user( a number input) and then display the fibonacci series that goes with that number.
    Any help would be greatly appreciated. Thanks


    Here is my code
    import java.util.Scanner;
     
     
    public class fibonacci {
     
    	public static void main(String args[]){
     
     
    		Scanner num = new Scanner(System.in);
    		System.out.println(num.nextLine());
     
    		System.out.println("  Fibonacci Series  ");
    		int f1, f2=0, f3=1;
    		for(int i=1;i<=num;i++){
    		System.out.print(" "+f3+" ");
    		f1 = f2;
    		f2 = f3;
    		f3 = f1 + f2;
    		}
    		}
    		}

    I get this error:

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The operator <= is undefined for the argument type(s) int, Scanner

    at fibonacci.main(fibonacci.java:14)


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Fibonacci series

    Can only use operators such as ==, >=, < on numeric types, and a Scanner object doesn't constitute a numeric type.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Fibonacci series

    Hey Newbie thanks for the reply,

    So what then would i use to take a number (int) in as a input?

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Fibonacci series

    If you read the API for Scanner, you'll notice a method named nextInt();
    Look it up, as It should be sufficient as to what you need.
    Scanner API
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  5. The Following User Says Thank You to newbie For This Useful Post:

    seanman (September 11th, 2011)

  6. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Fibonacci series

    Hey newbie thanks for the help i got it working now.

Similar Threads

  1. problem to get Fibonacci series- please help.
    By zoala001 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 3rd, 2011, 12:10 AM
  2. Fibonacci Spiral Help?
    By cmh0114 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 12th, 2010, 08:21 PM
  3. Replies: 1
    Last Post: October 19th, 2009, 11:53 PM
  4. [SOLVED] displaying sum of harmonic series
    By sriraj.kundan in forum Algorithms & Recursion
    Replies: 2
    Last Post: June 15th, 2009, 11:42 PM
  5. [SOLVED] Problem in generating Fibonacci sequence in java
    By big_c in forum Algorithms & Recursion
    Replies: 2
    Last Post: April 24th, 2009, 08:52 AM