• 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Problem with code, 'else' without 'if'

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! I have a problem with my code.
Can anyone help me?

Code:


Here is the code:

PLEASE HELP!
 
Master Rancher
Posts: 3313
31
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Sheriff
Posts: 13675
226
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

As Mike has shown, you have to watch for those stray semi-colons. You don't want them breaking up your if-else statements.
 
Bartender
Posts: 6115
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This typically means you have a semi-colon (;) after the if(), like:
The semi-colon by itself tells the compiler that you want an empty statement, which in most cases, you don't, especial with a following 'else'.
 
Lipt Codder
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Simmons wrote:




Thank You!!
 
Lipt Codder
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:This typically means you have a semi-colon (;) after the if(), like:
The semi-colon by itself tells the compiler that you want an empty statement, which in most cases, you don't, especial with a following 'else'.



Thanks for help, too. :3
 
Lipt Codder
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Welcome to the Ranch!

As Mike has shown, you have to watch for those stray semi-colons. You don't want them breaking up your if-else statements.



 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using this way::

 
Marshal
Posts: 65365
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sherin Mathew wrote:Try using this way . . .

Once you have so many if‑elses, you might be better off with a different construct. Try...or...
 
Marshal
Posts: 7070
491
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lipt Codder wrote:if (number1 == 1)
           out.println("Z0NK!");
       else if (number1 == 2) ;
       out.println("Z0NK2!");
       else if (number1 == 3)
           out.println("Z0NK3!");


If you were disciplined adding curly braces, such error less likely would have happened.

Demonstrating:

That just explains how cutting corners and trying to save few keystrokes and few lines of code leads you to the bugs you faced and hence spent way more time to solve the problem (assuming that's happened) than you really could have spent if were more pedantic and disciplined.

Don't import java.lang.System.*, just use System.out.print() as it is a more conventional way. Probably more developers than not got used to such construct.

Pay attention to indentation. Your line 24 is off. Poor indentation also leads to bugs. If that's taking too much time for you, use automated tools to format your code. Research on search engines how that can be done. You also have some excessive amount of empty lines near the end of source code - that also contributes to a messiness.

Why you call variables j and t? What do they represent? Name them so they tell you something. Variables as i, j, k usually are used for counter purposes within the loops (and in particular nested ones, which also are a poor practice, so better extract them to methods).
 
Sheriff
Posts: 6163
163
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:Pay attention to indentation. Your line 24 is off. Poor indentation also leads to bugs. If that's taking too much time for you, use automated tools to format your code. Research on search engines how that can be done.


You could also look at HowToFormatCode (that's a link).
 
She said she got a brazillian. I think owning people is wrong. That is how I learned ... tiny ad:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!