• Post Reply Bookmark Topic Watch Topic
  • New Topic

If...Else  RSS feed

 
Ja Shields
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all,

I know you have looked at code to distinguish leap years before so thanks for putting up with me. (This is an assignment which is why it shows up every so often and we can't just use what Java has for it.) I know I could beg, borrow, and steal what I have found for this code, but I really just want to know why mine won't compile. It complains that on both "else" statements, I don't have an "if." I know it has to be something really easy that I am missing, but I just don't see it. Help!

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Something easy:



should be



Putting the semicolon ends the if/then else block. In Java's mind, this is an empty if, followed by a code block followed by this random looking else.
 
Ja Shields
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was an easy (and dumb) mistake! Thanks!

New question - it seems to stall out when processing. Are while and if/else very time consuming processes? Should I go with a different method?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They aren't at all slow. However, I don't see why you are using a loop at all. Once you enter the loop, when will you ever leave it? i never changes.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be precise: The semi-colons after the while and if statements in lines 7, 9 and 13 must be removed.
 
Ja Shields
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Definitely easy mistakes to fix. Thanks for the help! I was able to get it to compile and run correctly for these purposes with the code below. (I always listen to advice though so feel free to suggest improvements. I am just proud that I am able to come up with these and get them to run!! )

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ja,
That's great! I only have one suggestion. Try to use a meaningful variable name. For example, "year" instead of "i". Also, "i" is commonly used as a variable in a for loop so it is good not to use that name for other purposes.

Obviously, this doesn't matter for a 20 line program. I'm mentioning it so you know before you write a lot of code and can develop good habits.

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ja Shields wrote:I always listen to advice though so feel free to suggest improvements.

OK, I do have one. It's not wildly important, but it is worth thinking about.

When you have compound conditions, it's usually worth trying to eliminate the most likely ones first, for example:Do you see how that might affect exectution? With one 'if', you've removed 75% of all cases, and with two, you've eliminated 99% of them; the last one is simply to sort out the '% 400' rule.

I'm not a big fan of negative logic, but in this case it really does help to streamline the process.

HIH

Winston
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ja

Another good habit to get into is writing very short main methods i.e. do not include any logic in them. That way, your class Leap could be used by another class.

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Boswell wrote:Another good habit to get into is writing very short main methods i.e. do not include any logic in them. That way, your class Leap could be used by another class.

Totally agree with James; but I'd also recommend writing methods that return something where its apllicable. In this case:
Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!