Hi guys I am quite new to java, and been trying to make a fibonacci number program just to learn more.
However I have search the web seen examples and more and I still cant get run this code, there seems to be something wrong with>

int N = Integer.parseInt(args [0]), in soecific with the index of the array.

Could someone tell me what is it?

Thanks a lot

public class Main {

   public static void main(String[] args) { 
      int N = Integer.parseInt(args[0]);
      int f = 0, g = 1;

      for (int i = 1; i <= N; i++) {
         f = f + g;
         g = f - g;
         System.out.println(f); 
      }
   }
}

Are you providing any arguments on the command line?

By arugments on the command line you mean if the user provides info like when you use a scanner?

If that is what you mean no, what I want to achieve is to print out the first N Fibonacci numbers.

thanks

i.e. java Fibonacci 8
* 1
* 1
* 2
* 3
* 5
* 8
* 13
* 21
* 34
* 55

That "8" in the above "example" is what command line arguments are. If "args[0]" is throwing an IndexOutOfBoundsException (which is the only "index of the array" problem that I know of), then you haven't provided any command line arguments.

thanks a lot for your help, but how i do that?

By typing it?

If you are using an IDE then read its documentation.

You mean by changing int N = Integer.parseInt(args[0]);
to int N = Integer.parseInt(args[8]); ??

I am trying to search beleive me but i dont get it, thanks and sorry for my stupidity

No. The arguments are everything after the name of the main class on the command line. Everything after the name of the main class on the command line is turned into a string array with the first item being index 0 of course. If you don't have anything after the name of the main class on the command line then you don't have any command line arguments and index 0 of the args array won't exist. The thing you need to read the documentation of your IDE for is to be able to find out how to enter command line arguments when executing from that IDE.

All right thanks a lot :)

Indeed I am quite slow, I fixed it thanks for the tip :)

I have one more doubt. Rather than putting the arguments is it possible to prompt the user to do this. I mean I have created a scanner object, however when I want the user to prompt the number of lines I do not know how since I do not know which variable to pass to the scanner. Any idea?

Thanks a lot

JOptionPane for a "GUI", for console simply system.out.println and "nextline" on the (hopefully) already defined scanner (or readLine on the bufferedreader depending on which is being used).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.