Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Can't figure out what's wrong with this  RSS feed

 
Joshua Corcoran
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Writing the following code dealing with Fibonacci numbers and can't figure out what's wrong



Keep getting theses errors:
----jGRASP exec: javac -g Fibonacci.java
Fibonacci.java:67: error: class, interface, or enum expected
      System.out.println("All done for now!");
      ^
Fibonacci.java:68: error: class, interface, or enum expected
      System.exit(0);
      ^
Fibonacci.java:70: error: class, interface, or enum expected
   }//end of the main method
   ^
3 errors

----jGRASP wedge: exit code for process is 1.
----jGRASP: operation complet

I know this is basic but I just started Java last week and I'm still not that good at it.
HELP ME PLEASE
 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Your curly braces are not balanced... Check to make sure that every open brace is matched with a closed one.

Henry
 
Joshua Corcoran
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I checked it over and I think they're all matched. But now, it's giving me this error:
----jGRASP exec: javac -g Fibonacci.java
Fibonacci.java:57: error: variable oldNumber might not have been initialized
      System.out.println("Fibonacci # " + (count+1) + " is " + (oldNumber + currentNumber));
                                                                ^
Fibonacci.java:57: error: variable currentNumber might not have been initialized
      System.out.println("Fibonacci # " + (count+1) + " is " + (oldNumber + currentNumber));
                                                                            ^
2 errors

----jGRASP wedge: exit code for process is 1.
----jGRASP: operation complete.

Still not sure what I did wrong. Any help would be much appreciated.
thank you!
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joshua Corcoran wrote:Fibonacci.java:57: error: variable oldNumber might not have been initialized


Sometimes you'll get misleading or confusing error messages, but this isn't one of them. It means just what it says. So:

At line 8 you declare the oldNumber variable but you don't assign it a value.

At line 34 you assign it a value of 0... but that only happens if count >= 0.

At line 45 you try to use the oldNumber variable. But what if count < 0? Then line 34 isn't executed and control arrives at line 45 with no value assigned to oldNumber.

That's what the error message is telling you. So you'll need to restructure your code so that oldNumber is assigned a value in all possible paths of execution before you actually try to use it.
 
Stephan van Hulst
Saloon Keeper
Posts: 7687
139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error messages explain exactly what's wrong. You are accessing variables oldNumber and currentNumber, but they may not have been initialized with a value yet.

This is caused because you declare variables without initializing them. You can avoid this by declaring variables only when you need them, and initializing them with the correct value immediately.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!