This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes a real noob Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "a real noob" Watch "a real noob" New topic
Author

a real noob

Daniel O'Neill
Greenhorn

Joined: Jan 15, 2005
Posts: 9
Ok, so I've scowered the internet, spent weeks working on it, and fidled with different ideas till I turned a skin kind of color and I'm out of ideas.
Why is it that there seems to be no tutorials between say 'HelloWorld' and 'Tetris', I never seem to find the information I am after...
Anyway, I am trying to write a simple(at least I thought it was) fibinacci number generator, my code is right here ->

I use netbeans, and this returns the error ->

This is my absolute first program ever since the days of line numbers, and I never had anybody to ask back in those days so figuring things out took a lot longer.
You may have suggestions on ways of improving my code, what I am doing wrong, and maybe even a url to teach me the intermediate stuff between 'HelloWorld' and say 'Tetris'.
Looking forward to getting something (anything) going - this program doesn't count - because I had to ask for help.
Dan.
Andris Jekabsons
Ranch Hand

Joined: Jan 20, 2004
Posts: 82
Your main() method is perfetly fine - iff you want it to be used by other classes, not JVM.
Java requires this method definition:
public static void main( String[] args ) {}
Regards
Sanjaya Sugiarto
Ranch Hand

Joined: Mar 25, 2004
Posts: 229
Your main method needs a String[] arguments.

Try:
public static void main(String[] args)


<a href="http://www.wi.hs-furtwangen.de" target="_blank" rel="nofollow">Business Information Technology - Hochschule Furtwangen University, Germany</a>
Andris Jekabsons
Ranch Hand

Joined: Jan 20, 2004
Posts: 82
By the way, read here what the String[] args is for.
Daniel O'Neill
Greenhorn

Joined: Jan 15, 2005
Posts: 9
Ok, so this now works

But this doesn't, just displays a long list of 0's

Why??
Daniel O'Neill
Greenhorn

Joined: Jan 15, 2005
Posts: 9
BTW, another question - and I'll see what I can find on the net right after I ask.
If I wanted it so that you typed 'fib.exe 1000' and made it so it displays only the first 1000, or 'fib.exe 2500' the first 2500, how would I do this, I guess it has something to do with the (Sting[] args) you are talking about, so if I wanted an integer would I just use (int[] args) instead??
Andris Jekabsons
Ranch Hand

Joined: Jan 20, 2004
Posts: 82
1. Java does not create a *.exe file, but *.class file. So you run your program like this:
> java fib 2500
2. if you want Java Virtual Machine to run your as a console application, you have to have this method as is (and only one per class):
public static void main( String [] args )
Please follow the link I provided in my previous reply. At the bottom of the page it will also tell you how to deal with integers (since Java doesn't know when you are entering integer values and when strings that just "look" like integers, it assumes you always give command line arguments as strings).
Daniel O'Neill
Greenhorn

Joined: Jan 15, 2005
Posts: 9
[/CODE]
package fib;

class fib
{
public static void main( String [] args )
{
long a=1;
long b=1;
int i=0;
int o=32768;
if (args.length > 0)
o=Integer.parseInt(args[0]);
for ( i=0 ; i < o ; i++ )
{
System.out.print(a + " ");
System.out.println();
a=a+b;
b=a;
}
}
}
[/CODE]
works, but only if I enter a low value for the parameter, anything over 100 just lists 0, is this an overflow error or something??, I was trying to use longs, so that I could make big numbers, but I have no idea what the 100th number in the fibinacci series is, any ideas why when I use 100 or more I just get 0's??
Daniel O'Neill
Greenhorn

Joined: Jan 15, 2005
Posts: 9
I answer my own question 63 is the last value it will take, the 64th is negetive, and 65 onwards is 0.
Thanx everyone for your help.
Andris Jekabsons
Ranch Hand

Joined: Jan 20, 2004
Posts: 82
Originally posted by Daniel O'Neill:
But this doesn't, just displays a long list of 0's ...
Why??

Actually, if you had a console with huge buffer, you'd see that that at first it works as desired. However, after a while it starts showing 0's.
First, in for ( i=0 ; i < 32767 ; i++ ) {} loop substitute 32767 with 70 and check the result.
With each iteration you assign a greater value to both variables a and b then in the previous loop. Note that a and b are int's. The maximum value of an int is 2^31-1 or java.lang.Integer.MAX_VALUE. Once you exceed that value, JVM will start counting from the minium value of an int (which is -2^31 or java.lang.Integer.MIN_VALUE). That's why you're getting a negative value, and eventually both a and b become 0's and at the end your program keeps adding 0 to 0 throughout the rest of the loop.
To avoid this, make a and b long variables, and make sure they never exceed the max value of long; smth like this:
for( i = 0; i < 32766 && a < Long.MAX_VALUE && b < Long.MAX_VALUE; i++ )
Andris
Andris Jekabsons
Ranch Hand

Joined: Jan 20, 2004
Posts: 82
If you use java.math.BigInteger, there is no limit to the length of your number, but that is probably better to be left after a couple more introductory Java classes.
Daniel O'Neill
Greenhorn

Joined: Jan 15, 2005
Posts: 9
Thanx again.
Dan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: a real noob
 
Similar Threads
New Free Online Java Compiler and Runner
What does recursion code hurt ?
fibonacci program
please help
Fibonacci Sum