473,378 Members | 1,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Two Syntax errors that I can't figure out

14
I am trying to to write a program that will print the first 15 Fibonacci numbers each on one line with to lessons (print and produce)

the code that I have is below:
class numbers{
static int numbers[] = new int[15];
public static void produce(int Fib){

if(Fib < 2)
numbers[n] = n;
else
numbers[n] = numbers[n-1] + numbers[n-2];
}

public static void print(){

for(int k = 0; k < 15; k++)
System.out.println(numbers[k]);
}

public static void main(args[0]){
for(Integer i = 0; i < 15; i++)

produce (i);
print ();

}
the errors are
1) public static void main(args[0])
telling me an error under the "0"

and the other is I am missing and indentifier
Nov 16 '06 #1
3 1087
Ganon11
3,652 Expert 2GB
I am trying to to write a program that will print the first 15 Fibonacci numbers each on one line with to lessons (print and produce)

the code that I have is below:
Expand|Select|Wrap|Line Numbers
  1. class numbers{
  2.     static int numbers[] = new int[15];
  3.       public static void produce(int Fib){
  4.  
  5.     if(Fib < 2)
  6.       numbers[n] = n; // Where is N declared?
  7.     else
  8.       numbers[n] = numbers[n-1] + numbers[n-2];
  9.     }
  10.  
  11.     public static void print(){
  12.  
  13.     for(int k = 0; k < 15; k++)
  14.       System.out.println(numbers[k]);
  15.     }
  16.  
  17.     public static void main(args[0]){ // Should be "public static void main(String args[]) {
  18.       for(Integer i = 0; i < 15; i++)
  19.  
  20.     produce (i);
  21.     print ();
  22.  
  23.     }
the errors are
1) public static void main(args[0])
telling me an error under the "0"

and the other is I am missing and indentifier
You never declared N, yet you use it. Maybe you should use Fib instead of N.

Also, the main() function expects a class type, then a name - args[] was the name, and there was no type with it. It should be String args[]
Nov 16 '06 #2
Hello

Your code only needs small modification:

Expand|Select|Wrap|Line Numbers
  1.  
  2. class numbers{
  3. static int numbers[] = new int[15];
  4. public static void produce(int n){
  5.  
  6. if(n < 2)
  7. numbers[n] = n;
  8. else
  9. numbers[n] = numbers[n-1] + numbers[n-2];
  10. }
  11.  
  12. public static void print(){
  13.  
  14. for(int k = 0; k < 15; k++)
  15. System.out.println(numbers[k]);
  16. }
  17.  
  18. public static void main(String args[]){
  19. for(Integer i = 0; i < 15; i++)
  20.  
  21. produce (i);
  22. print ();
  23. }}
  24.  
alternatively you could do it like so with only one loop using a recursive method

Expand|Select|Wrap|Line Numbers
  1.  
  2. class Fibonacci {
  3.     public static int fib(int n) {
  4.         if (n == 0) return 0;
  5.         if (n == 1) return 1;
  6.         return fib(n-1)+fib(n-2);
  7.     }
  8.  
  9.     public static void main(String[] args) {
  10.     for (int i=0; i<15; i++)
  11.         System.out.print(fib(i)+", ");
  12. }
  13.  
hope that helps

mark
Nov 17 '06 #3
hjc
14
Thank you both very much for your help!!!! I got it working



Hello

Your code only needs small modification:

Expand|Select|Wrap|Line Numbers
  1.  
  2. class numbers{
  3. static int numbers[] = new int[15];
  4. public static void produce(int n){
  5.  
  6. if(n < 2)
  7. numbers[n] = n;
  8. else
  9. numbers[n] = numbers[n-1] + numbers[n-2];
  10. }
  11.  
  12. public static void print(){
  13.  
  14. for(int k = 0; k < 15; k++)
  15. System.out.println(numbers[k]);
  16. }
  17.  
  18. public static void main(String args[]){
  19. for(Integer i = 0; i < 15; i++)
  20.  
  21. produce (i);
  22. print ();
  23. }}
  24.  
alternatively you could do it like so with only one loop using a recursive method

Expand|Select|Wrap|Line Numbers
  1.  
  2. class Fibonacci {
  3.     public static int fib(int n) {
  4.         if (n == 0) return 0;
  5.         if (n == 1) return 1;
  6.         return fib(n-1)+fib(n-2);
  7.     }
  8.  
  9.     public static void main(String[] args) {
  10.     for (int i=0; i<15; i++)
  11.         System.out.print(fib(i)+", ");
  12. }
  13.  
hope that helps

mark
Nov 18 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
22
by: Tuang | last post by:
I'm checking out Python as a candidate for replacing Perl as my "Swiss Army knife" tool. The longer I can remember the syntax for performing a task, the more likely I am to use it on the spot if...
24
by: Andrew Koenig | last post by:
PEP 315 suggests that a statement such as do: x = foo() while x != 0: bar(x) be equivalent to while True:
22
by: Phil Powell | last post by:
<script> <!-- function isValidAlert() { for (var i = 0; i < document.alertForm.length; i++) { with (document.alertForm.elements) { if (.name == "text" || .name == "password" || .name ==...
2
by: Daniel | last post by:
how to make sure a xsl document has valid xsl syntax? i tried loading it into an xml document but that doesnt show syntax errors inside attributes such as "foo/bar" vs "bar\foo"
11
by: JV | last post by:
I just converted a big web project from VB.NET to C# and I got some very strange runtime errors. Apparently, throughout the ASPX pages, the VB version of this project used the expression <%#...
6
by: Scott Nixon | last post by:
New to Postgres 7.3 from 7.0. Am having some trouble with a query that worked in 7.0 but not in 7.3.....can't seem to figure out the syntax or find info about how to do this anywhere. ...
1
by: Hari Sekhon | last post by:
I've written an except hook into a script as shown below which works well for the most part and catches exceptions. import sys def myexcepthook(type,value,tb): do something ...
6
by: Howard | last post by:
Hi, I have a function in three unrelated but similar classes. The code in the member functions is identical for all three classes. What I want is to make a template which defines the function,...
6
Soniad
by: Soniad | last post by:
Hello, I am excecuting a stored procedure in my ASP page , it has one out parameter (@confirm) . after executing the procedure i want to retreive this out parameter and assign it to variable...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.