6 Replies - 1348 Views - Last Post: 09 April 2010 - 10:01 PM Rate Topic: -----

#1 huskers23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-July 09

Creating Java program calling one static method for fibonacci numbers

Post icon  Posted 05 July 2009 - 12:52 PM

I am in a intro computer class and need help on an assignment. I have to create a program that has one main method and calls another static method to figure the fibonacci # that is input by the user. I can not use a recuring solution. here is what I have. the program complies but when I try a sample I get and error
Which Fibonacci number would you like?
8
Fibonacci #Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier 'd'
at java.util.Formatter.format(Formatter.java:2431)
at java.io.PrintStream.format(PrintStream.java:920)
at java.io.PrintStream.printf(PrintStream.java:821)
at mpenke_Fibonacci.main(mpenke_Fibonacci.java:37)
[code]
import java.util.Scanner;

public class mpenke_Fibonacci
{
public static void main ( String args[] )
{
int fib;
Scanner input = new Scanner (System.in);
do
{
System.out.print("Which Fibonacci number would you like?\n");
fib = input.nextInt();
}
while (fib <=0 && fib >= 70);

double num = fibcalc( fib );

System.out.printf ("Fibonacci #%d is" + num);
}

public static double fibcalc( int fib )
{
if ( fib <= 2)
return fib;

double f0 = 0;
double f1 = 1;
double current = 1;

for (int i = 2; i <= 70; i++)
{
current = f0 + f1;
f0 = f1;
f1 = current;
}
return current;
}
}
[code/]
can anyone help find my problem?

Is This A Good Question/Topic? 0
  • +

Replies To: Creating Java program calling one static method for fibonacci numbers

#2 AbuJaFaR  Icon User is offline

  • D.I.C Regular

Reputation: 13
  • View blog
  • Posts: 330
  • Joined: 13-December 07

Re: Creating Java program calling one static method for fibonacci numbers

Posted 05 July 2009 - 01:02 PM

Hey, fix the second code tag :)

Secondly, this line : System.out.printf ("Fibonacci #%d is" + num);
must change to this : System.out.printf ("Fibonacci #%d is %.2f",fib,num);

If u are using printf instead of + place a comma ( , ) .And lastly, d is for integers.Change it to %.2f or whatever precision u want.

This post has been edited by AbuJaFaR: 05 July 2009 - 01:05 PM

Was This Post Helpful? 0
  • +
  • -

#3 huskers23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-July 09

Re: Creating Java program calling one static method for fibonacci numbers

Posted 05 July 2009 - 01:10 PM

Also I am only allowed one return statement!
Was This Post Helpful? 0
  • +
  • -

#4 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Creating Java program calling one static method for fibonacci numbers

Posted 05 July 2009 - 01:15 PM

So just return current. If you are returning fib because a number less than that is not allowed, just handle you exception there instead of returning the value.
Was This Post Helpful? 0
  • +
  • -

#5 huskers23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-July 09

Re: Creating Java program calling one static method for fibonacci numbers

Posted 05 July 2009 - 01:18 PM

View Posthuskers23, on 5 Jul, 2009 - 12:10 PM, said:

Also I am only allowed one return statement!

import java.util.Scanner;

		public class mpenke_Fibonacci
		{
				public static void main ( String args[] )
				{
						int fib;
						Scanner input = new Scanner (System.in);
						do
						{
						System.out.print("Which Fibonacci number would you like?\n");
						fib = input.nextInt();
						}
						while (fib <=0 && fib >= 70);

						double num = fibcalc( fib );

						System.out.printf ("Fibonacci #%d is %f",fib,num);
				}

				public static double fibcalc( int fib )
				{
						if ( fib <= 2)
						return fib;

						double f0 = 0;
						double f1 = 1;
						double current = 1;

						for (int i = 2; i <= 70; i++)
						{
								current = f0 + f1;
								f0 = f1;
								f1 = current;
						}
						return current;
				}
		}




I got it to run. I tested it with the number 8 input which should out put 21. instead it output 190392490709135.00000 which is what the answer would be if I input 70. also can cannot output any decimal places.

This post has been edited by huskers23: 05 July 2009 - 01:20 PM

Was This Post Helpful? 0
  • +
  • -

#6 Guest_tyler*


Reputation:

Re: Creating Java program calling one static method for fibonacci numbers

Posted 09 April 2010 - 04:35 PM

The above code was on the right track but should be done like this



import java.util.Scanner;

public class mpenke_Fibonacci
{
public static void main ( String args[] )
{
int fib;
Scanner input = new Scanner (System.in);
do
{
System.out.print("Which Fibonacci number would you like?\n");
fib = input.nextInt();
}
while (fib <=0 && fib >= 70);

double num = fibcalc( fib );

System.out.printf ("Fibonacci #%d is %f",fib,num);
}

public static double fibcalc( int fib )
{
//CHANGED THIS if ( fib < 2)
fib = fib;

//CHANGED THIS double f0 = 1;
//CHANGED THIS double f1 = 0;
double fib = 1;

// CHANGED THIS for (int i = 2; i <= fib; i++)
{
fib = f0 + f1;
f0 = f1;
f1 = fib;
}
return fib;
}
}

these corrections should make the program run perfectly
Was This Post Helpful? 0

#7 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: Creating Java program calling one static method for fibonacci numbers

Posted 09 April 2010 - 10:01 PM

Thanks for helping, but:
1) This post is a year old
2) We don't just "hand out solutions here" - we help them learn
3) :code:
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1