Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Leap Year program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Leap Year program" Watch "Leap Year program" New topic
Author

Leap Year program

Benjamin Chau
Ranch Hand

Joined: Oct 05, 2008
Posts: 49
Ok, this Leap Year program is really starting to get to me. While seemingly simple, here are the error messages I get when I try to compile.

C:\Documents and Settings\Benjamin Q. Chau\WRITTENPROGRAMS>javac Leap.java
Leap.java:13: '.class' expected
if (boolean == true)
^
Leap.java:28: ')' expected
^
Leap.java:29: illegal start of expression
}
^
3 errors

Why would I need to tag on ".class" to the boolean keyword? Overall, I'm not understanding why I'm getting these error messages. I checked and checked to make sure I closed all brackets/parantheses. Any help would be great. Here's my code:


[ November 17, 2008: Message edited by: Benjamin Chau ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60781
    
  65

Please take the time to choose the correct forum for your posts. This forum is for questions on HTML.

For more information, please read this.

This post has been moved to a more appropriate forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Benjamin Chau
Ranch Hand

Joined: Oct 05, 2008
Posts: 49
Oh sorry Bear, didn't realize what forum I was in...

Benjamin
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

@ Benjamin Chau



In the above statement you are check year which is a int.And it does NOT result in boolean value. && operator looks for something which results in boolean(true or false) ONLY.

And what is that you are checking by 100 != 100??? Is not at all need ...

And i doubt the condition that you are checking is correrct.

Try this..
boolean isLeapYear = ((year % 4 == 0) ||(year % 400 == 0));


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Benjamin Chau
Ranch Hand

Joined: Oct 05, 2008
Posts: 49
James,

Thanks for pointing out those errors. Actually, I still need that middle condition because that partly defines a Leap Year so I left it in. Here's the error message I get now when I try to compile:


C:\Documents and Settings\Benjamin Q. Chau\WRITTENPROGRAMS>javac Leap.java
Leap.java:13: '.class' expected
if (boolean == true)
^
Leap.java:28: ')' expected
^
Leap.java:29: illegal start of expression
}
^
3 errors



And here's my modified code:



[ November 17, 2008: Message edited by: Benjamin Chau ]
[ November 18, 2008: Message edited by: Benjamin Chau ]
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580



I think boolean should be replaced by isLeapYear.
boolean is a data type ... you can't do what you are trying there

do this..
if (isLeapYear==true)
Benjamin Chau
Ranch Hand

Joined: Oct 05, 2008
Posts: 49
That's it! YOU'RE AWESOME! THANKS! Don't know why I used the boolean variable that way...

Benjamin
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38003
    
  22
"boolean" isn't a variable, but a keyword. You mustn't used keywords or other reserved words out of their intended context.

"== false" and "== true" are errors. If you write = false by mistake. you can get all sorts of errors. You write if(leapYear) or if(!leapYear).
Benjamin Chau
Ranch Hand

Joined: Oct 05, 2008
Posts: 49
Thanks Campbell. That was silly of me to use boolean as a variable. These late nighters screw with your mind I guess. I'm going back to edit the code now.

James suggested I change the syntax around a bit in one of his earlier posts but I've been thinking that that would change the logic and make the Leap Year program not work correctly. So I took that into account in my editing...

Benjamin
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38003
    
  22
It ought to readNever use == false or == true because you can get nasty errors if you write = instead of == by mistake.
 
Consider Paul's rocket mass heater.
 
subject: Leap Year program
 
Similar Threads
unreachable statement error
Where are the dates
two puzzling errors
please verify this code
Help with parsing integers from a string