• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

java problem with boolean method

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello guys i have a private boolean method and i am trying to use in in some other methods and it always says cannot int with boolean

private  boolean isLeapYear(int year) {
       int _year=year;
       if (_year % 4 != 0) {
           return false;
       } else if (_year % 400 == 0) {
           return true;
       } else if (_year % 100 == 0) {
           return false;
       } else {
           return true;
       }
   }


this is the boolean method^^^^^^^^


and this is my constructor please help me to fix it



//constuctor to set date if valid if no set default 1/1/2000
   public Date( int day, int month, int year) {
       int _year;
       int _day;
       int _month;
     
               
   
       if((month == 1 || month == 3 || month == 5 || month == 7 ||
           month == 8 ||month == 10 || month == 12 )||day<=1||day>=31){
           _day=day;}
       else {
           _day=1;}
       if((month == 4 ||month == 6 ||month == 9 ||month == 11)||
       day<=1||day>=30){
           _day=day;}
       else{
           _day=1;}
       if (month=2&&isLeapYear(_year)=true&&day<=1&&day>=29){//incompatible types int cannot convert into boolean
           _day=day;}
       else if(month=2&&isLeapYear(_year)=false&&day<=1&&day>=28){//incompatible types int cannot convert into boolean
           _day=day;}
       else{
           _day=1;}
       if (month<=1||month>=12){
           _month=month;}
       else{
           _month=1;}
       if (year<=2000||year<=2030){
           _year=year;}
       else{
           _year=2000;}
   }
 
lowercase baba
Posts: 13014
66
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


 if (month=2&&isLeapYear(_year)=true&&day<=1&&day>=29)

you realize that a single '=' is an assignment, not a comparison?  so you are trying to assign the value of "true"  to what you got back from your method call...which is just wrong.
You are also assigning the value of 2 to month, and then trying to use the "2" in a boolean expression.  

Also, you should never compare a boolean to "true" or "false",  you just use it.  You probably need something like:

 if (month==2 && isLeapYear(_year) && day<=1 && day>=29)
 
Jenia Levin
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks alot man this is so helpfull ( i am a java begginer and kind of hard to figure out everything
 
fred rosenberger
lowercase baba
Posts: 13014
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a common error everyone makes at some point.  One "trick" to reduce it is to reverse your comparisons, putting the constant first...Instead of

if (month==2)

you do

if (2==month)

That way, if you miss one of the '=', you'll get a compile time error.
 
Marshal
Posts: 74381
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Surely you will get a compile‑time error whichever way round you have the two operands for the ==? If you try if (month = 2) ..., you will get an “Expected boolean, found int” error message.

if (month==2 && isLeapYear(_year) && day<=1 && day>=29)

I presume, with the <= and >= that way round, you are going to return false?

The usual convention is, don't use underscores in variable names: use this.year to disambiguate the field from the parameter.
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

if((month == 1 || month == 3 || month == 5 || month == 7 ||
          month == 8 ||month == 10 || month == 12 )||day<=1||day>=31){
          _day=day;}


If the user enters 0 for day, this line will set _day to 0 regardless of the month.

Perhaps you mean to use something like

 
Saloon Keeper
Posts: 8760
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
fred rosenberger
lowercase baba
Posts: 13014
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:


Shouldn't you add in a check for year > 1752?
 
Sheriff
Posts: 16675
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:Shouldn't you add in a check for year > 1752?


If you're looking to tackle scope creep, sure. But why stop at 1752? You'd have to add locale, too, since moving to Gregorian calendar didn't happen universally at the same time, at least not according to this: https://en.wikipedia.org/wiki/Old_Style_and_New_Style_dates
 
reply
    Bookmark Topic Watch Topic
  • New Topic