• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can't figure out what's wrong with this

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 15491
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic