This week's book giveaway is in the Artificial Intelligence forum.
We're giving away four copies of Pragmatic AI and have Noah Gift on-line!
See this thread for details.
Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Else giving error  RSS feed

 
Greenhorn
Posts: 11
Eclipse IDE Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only thing in this code that is getting errors is else.
Why?

 
author
Sheriff
Posts: 23571
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You forgot to close the "if" block, hence, the "else" can't be matched to an "if".

Henry
 
Author and ninkuma
Marshal
Posts: 66789
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Proper indentation isn't just "making code look nice", it's a necessary tool to understanding and finding problems with the structure of code.
 
lowercase baba
Bartender
Posts: 12627
50
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look VERY carefully at your if statement. it actaully is closed, but not on the line you think. So by the time you get to the else, the compiler says "but there's no if for this to go with..."
 
Nathan Tibbitts
Greenhorn
Posts: 11
Eclipse IDE Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henry Wong wrote:You forgot to close the "if" block, hence, the "else" can't be matched to an "if".

Henry


I closed the If block using a curly brace. }
It still shows up with an error giving no other token besides syntax error on token "else". delete this token.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66789
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Proper indentation isn't just "making code look nice", it's a necessary tool to understanding and finding problems with the structure of code.


Fix. The. Indentation.
 
Henry Wong
author
Sheriff
Posts: 23571
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You also have a semicolon problem with the "if" block.

Henry
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66789
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henry Wong wrote:You also have a semicolon problem with the "if" block.


That too.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12627
50
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nathan Tibbitts wrote:
I closed the If block using a curly brace. }


on this line:

the if-block is closed by that semi-colon. you then have an open curlie that is not associated with the if in any way. It's the exact same this as if you had this:


but even if you take that out, you have problems.  Which is why Bear keeps saying "fix the indentation".  you would have this:




which corrected looks like this:



your if block is NOT properly closed before you hit the else statement.
 
Bartender
Posts: 619
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You really need to fix up[ your indentation. Some of your lines are indented 4-spaces, some three, and some 2. Even if you remove the extraneous semicolon and add the proper close for the if-block, you will still have unmatched braces. Indent everything exactly the same number of spaces and you should find the problem.
 
Marshal
Posts: 59783
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you have got the indentation and formatting right: why are you declaring the result as a double? Fibonacci numbers are integers. You can probably use an int, but for results ≥ 2³¹ use longs or BigIntegers.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!